<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <title>Taskflow Algorithms &raquo; Task-parallel Pipeline | Taskflow QuickStart</title>
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,400i,600,600i%7CSource+Code+Pro:400,400i,600" />
  <link rel="stylesheet" href="m-dark+documentation.compiled.css" />
  <link rel="icon" href="favicon.ico" type="image/x-icon" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta name="theme-color" content="#22272e" />
</head>
<body>
<header><nav id="navigation">
  <div class="m-container">
    <div class="m-row">
      <span id="m-navbar-brand" class="m-col-t-8 m-col-m-none m-left-m">
        <a href="https://taskflow.github.io"><img src="taskflow_logo.png" alt="" />Taskflow</a> <span class="m-breadcrumb">|</span> <a href="index.html" class="m-thin">QuickStart</a>
      </span>
      <div class="m-col-t-4 m-hide-m m-text-right m-nopadr">
        <a href="#search" class="m-doc-search-icon" title="Search" onclick="return showSearch()"><svg style="height: 0.9rem;" viewBox="0 0 16 16">
          <path id="m-doc-search-icon-path" d="m6 0c-3.31 0-6 2.69-6 6 0 3.31 2.69 6 6 6 1.49 0 2.85-0.541 3.89-1.44-0.0164 0.338 0.147 0.759 0.5 1.15l3.22 3.79c0.552 0.614 1.45 0.665 2 0.115 0.55-0.55 0.499-1.45-0.115-2l-3.79-3.22c-0.392-0.353-0.812-0.515-1.15-0.5 0.895-1.05 1.44-2.41 1.44-3.89 0-3.31-2.69-6-6-6zm0 1.56a4.44 4.44 0 0 1 4.44 4.44 4.44 4.44 0 0 1-4.44 4.44 4.44 4.44 0 0 1-4.44-4.44 4.44 4.44 0 0 1 4.44-4.44z"/>
        </svg></a>
        <a id="m-navbar-show" href="#navigation" title="Show navigation"></a>
        <a id="m-navbar-hide" href="#" title="Hide navigation"></a>
      </div>
      <div id="m-navbar-collapse" class="m-col-t-12 m-show-m m-col-m-none m-right-m">
        <div class="m-row">
          <ol class="m-col-t-6 m-col-m-none">
            <li><a href="pages.html">Handbook</a></li>
            <li><a href="namespaces.html">Namespaces</a></li>
          </ol>
          <ol class="m-col-t-6 m-col-m-none" start="3">
            <li><a href="annotated.html">Classes</a></li>
            <li><a href="files.html">Files</a></li>
            <li class="m-show-m"><a href="#search" class="m-doc-search-icon" title="Search" onclick="return showSearch()"><svg style="height: 0.9rem;" viewBox="0 0 16 16">
              <use href="#m-doc-search-icon-path" />
            </svg></a></li>
          </ol>
        </div>
      </div>
    </div>
  </div>
</nav></header>
<main><article>
  <div class="m-container m-container-inflatable">
    <div class="m-row">
      <div class="m-col-l-10 m-push-l-1">
        <h1>
          <span class="m-breadcrumb"><a href="Algorithms.html">Taskflow Algorithms</a> &raquo;</span>
          Task-parallel Pipeline
        </h1>
        <nav class="m-block m-default">
          <h3>Contents</h3>
          <ul>
            <li><a href="#TaskParallelPipelineIncludeHeaderFile">Include the Header</a></li>
            <li><a href="#UnderstandPipelineScheduling">Understand the Pipeline Scheduling Framework</a></li>
            <li><a href="#CreateATaskParallelPipelineModuleTask">Create a Task-parallel Pipeline Module Task</a></li>
            <li>
              <a href="#ConnectWithTasks">Connect Pipeline with Other Tasks</a>
              <ul>
                <li><a href="#IterateAPipeline">Example 1: Iterate a Pipeline</a></li>
                <li><a href="#ConcatenateTwoPipelines">Example 2: Concatenate Two Pipelines</a></li>
                <li><a href="#DefineMultipleTaskParallelPipelines">Example 3: Define Multiple Parallel Pipelines</a></li>
              </ul>
            </li>
            <li><a href="#ResetPipeline">Reset a Pipeline</a></li>
            <li><a href="#TaskParallelPipelineLearnMore">Learn More about Taskflow Pipeline</a></li>
          </ul>
        </nav>
<p>Taskflow provides a <em>task-parallel</em> pipeline programming framework for you to implement a pipeline algorithm. Pipeline parallelism refers to a parallel execution of multiple data tokens through a linear chain of pipes or stages. Each stage processes the data token sent from the previous stage, applies the given callable to that data token, and then sends the result to the next stage. Multiple data tokens can be processed simultaneously across different stages.</p><section id="TaskParallelPipelineIncludeHeaderFile"><h2><a href="#TaskParallelPipelineIncludeHeaderFile">Include the Header</a></h2><p>You need to include the header file, <code>taskflow/algorithm/pipeline.hpp</code>, for implementing task-parallel pipeline algorithms.</p><pre class="m-code"><span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;taskflow/algorithm/pipeline.hpp&gt;</span></pre></section><section id="UnderstandPipelineScheduling"><h2><a href="#UnderstandPipelineScheduling">Understand the Pipeline Scheduling Framework</a></h2><p>A <a href="classtf_1_1Pipeline.html" class="m-doc">tf::<wbr />Pipeline</a> object is a <em>composable</em> graph to create a <em>pipeline scheduling framework</em> through a module task in a taskflow (see <a href="ComposableTasking.html" class="m-doc">Composable Tasking</a>). Unlike the conventional pipeline programming frameworks (e.g., Intel TBB Parallel Pipeline), Taskflow&#x27;s pipeline algorithm does not provide any data abstraction, which often restricts users from optimizing data layouts in their applications, but a flexible framework for users to customize their application data atop an efficient pipeline scheduling framework.</p><div class="m-graph"><svg style="width: 23.800rem; height: 33.300rem;" viewBox="0.00 0.00 238.00 333.00">
<g transform="scale(1 1) rotate(0) translate(4 329)">
<title>Taskflow</title>
<g class="m-cluster">
<title>cluster0</title>
<polygon stroke-dasharray="5,2" points="8,-245.75 8,-317 222,-317 222,-245.75 8,-245.75"/>
<text text-anchor="middle" x="115" y="-303.5" font-family="Helvetica,sans-Serif" font-size="10.00">line 0</text>
</g>
<g class="m-cluster">
<title>cluster1</title>
<polygon stroke-dasharray="5,2" points="8,-166.5 8,-237.75 222,-237.75 222,-166.5 8,-166.5"/>
<text text-anchor="middle" x="115" y="-224.25" font-family="Helvetica,sans-Serif" font-size="10.00">line 1</text>
</g>
<g class="m-cluster">
<title>cluster2</title>
<polygon stroke-dasharray="5,2" points="8,-87.25 8,-158.5 222,-158.5 222,-87.25 8,-87.25"/>
<text text-anchor="middle" x="115" y="-145" font-family="Helvetica,sans-Serif" font-size="10.00">line 2</text>
</g>
<g class="m-cluster">
<title>cluster3</title>
<polygon stroke-dasharray="5,2" points="8,-8 8,-79.25 222,-79.25 222,-8 8,-8"/>
<text text-anchor="middle" x="115" y="-65.75" font-family="Helvetica,sans-Serif" font-size="10.00">line 3</text>
</g>
<g class="m-node m-flat">
<title>p00</title>
<ellipse cx="43" cy="-271.75" rx="27" ry="18"/>
<text text-anchor="middle" x="43" y="-267.88" font-family="Helvetica,sans-Serif" font-size="10.00">pipe&#45;0</text>
</g>
<g class="m-node m-flat">
<title>p10</title>
<ellipse cx="43" cy="-192.5" rx="27" ry="18"/>
<text text-anchor="middle" x="43" y="-188.62" font-family="Helvetica,sans-Serif" font-size="10.00">pipe&#45;0</text>
</g>
<g class="m-edge">
<title>p00&#45;&gt;p10</title>
<path d="M43,-253.61C43,-244.37 43,-232.73 43,-222.16"/>
<polygon points="46.5,-222.38 43,-212.38 39.5,-222.38 46.5,-222.38"/>
</g>
<g class="m-node m-flat">
<title>p01</title>
<ellipse cx="115" cy="-271.75" rx="27" ry="18"/>
<text text-anchor="middle" x="115" y="-267.88" font-family="Helvetica,sans-Serif" font-size="10.00">pipe&#45;1</text>
</g>
<g class="m-edge">
<title>p00&#45;&gt;p01</title>
<path d="M70.28,-271.75C72.28,-271.75 74.28,-271.75 76.29,-271.75"/>
<polygon points="76.24,-275.25 86.24,-271.75 76.24,-268.25 76.24,-275.25"/>
</g>
<g class="m-node m-flat">
<title>p20</title>
<ellipse cx="43" cy="-113.25" rx="27" ry="18"/>
<text text-anchor="middle" x="43" y="-109.38" font-family="Helvetica,sans-Serif" font-size="10.00">pipe&#45;0</text>
</g>
<g class="m-edge">
<title>p10&#45;&gt;p20</title>
<path d="M43,-174.36C43,-165.12 43,-153.48 43,-142.91"/>
<polygon points="46.5,-143.13 43,-133.13 39.5,-143.13 46.5,-143.13"/>
</g>
<g class="m-node m-flat">
<title>p11</title>
<ellipse cx="115" cy="-192.5" rx="27" ry="18"/>
<text text-anchor="middle" x="115" y="-188.62" font-family="Helvetica,sans-Serif" font-size="10.00">pipe&#45;1</text>
</g>
<g class="m-edge">
<title>p10&#45;&gt;p11</title>
<path d="M70.28,-192.5C72.28,-192.5 74.28,-192.5 76.29,-192.5"/>
<polygon points="76.24,-196 86.24,-192.5 76.24,-189 76.24,-196"/>
</g>
<g class="m-node m-flat">
<title>p30</title>
<ellipse cx="43" cy="-34" rx="27" ry="18"/>
<text text-anchor="middle" x="43" y="-30.12" font-family="Helvetica,sans-Serif" font-size="10.00">pipe&#45;0</text>
</g>
<g class="m-edge">
<title>p20&#45;&gt;p30</title>
<path d="M43,-95.11C43,-85.87 43,-74.23 43,-63.66"/>
<polygon points="46.5,-63.88 43,-53.88 39.5,-63.88 46.5,-63.88"/>
</g>
<g class="m-node m-flat">
<title>p21</title>
<ellipse cx="115" cy="-113.25" rx="27" ry="18"/>
<text text-anchor="middle" x="115" y="-109.38" font-family="Helvetica,sans-Serif" font-size="10.00">pipe&#45;1</text>
</g>
<g class="m-edge">
<title>p20&#45;&gt;p21</title>
<path d="M70.28,-113.25C72.28,-113.25 74.28,-113.25 76.29,-113.25"/>
<polygon points="76.24,-116.75 86.24,-113.25 76.24,-109.75 76.24,-116.75"/>
</g>
<g class="m-node m-flat">
<title>p31</title>
<ellipse cx="115" cy="-34" rx="27" ry="18"/>
<text text-anchor="middle" x="115" y="-30.12" font-family="Helvetica,sans-Serif" font-size="10.00">pipe&#45;1</text>
</g>
<g class="m-edge">
<title>p30&#45;&gt;p31</title>
<path d="M70.28,-34C72.28,-34 74.28,-34 76.29,-34"/>
<polygon points="76.24,-37.5 86.24,-34 76.24,-30.5 76.24,-37.5"/>
</g>
<g class="m-node m-flat">
<title>p02</title>
<ellipse cx="187" cy="-271.75" rx="27" ry="18"/>
<text text-anchor="middle" x="187" y="-267.88" font-family="Helvetica,sans-Serif" font-size="10.00">pipe&#45;2</text>
</g>
<g class="m-edge">
<title>p01&#45;&gt;p02</title>
<path d="M142.28,-271.75C144.28,-271.75 146.28,-271.75 148.29,-271.75"/>
<polygon points="148.24,-275.25 158.24,-271.75 148.24,-268.25 148.24,-275.25"/>
</g>
<g class="m-node m-flat">
<title>p12</title>
<ellipse cx="187" cy="-192.5" rx="27" ry="18"/>
<text text-anchor="middle" x="187" y="-188.62" font-family="Helvetica,sans-Serif" font-size="10.00">pipe&#45;2</text>
</g>
<g class="m-edge">
<title>p11&#45;&gt;p12</title>
<path d="M142.28,-192.5C144.28,-192.5 146.28,-192.5 148.29,-192.5"/>
<polygon points="148.24,-196 158.24,-192.5 148.24,-189 148.24,-196"/>
</g>
<g class="m-node m-flat">
<title>p22</title>
<ellipse cx="187" cy="-113.25" rx="27" ry="18"/>
<text text-anchor="middle" x="187" y="-109.38" font-family="Helvetica,sans-Serif" font-size="10.00">pipe&#45;2</text>
</g>
<g class="m-edge">
<title>p21&#45;&gt;p22</title>
<path d="M142.28,-113.25C144.28,-113.25 146.28,-113.25 148.29,-113.25"/>
<polygon points="148.24,-116.75 158.24,-113.25 148.24,-109.75 148.24,-116.75"/>
</g>
<g class="m-node m-flat">
<title>p32</title>
<ellipse cx="187" cy="-34" rx="27" ry="18"/>
<text text-anchor="middle" x="187" y="-30.12" font-family="Helvetica,sans-Serif" font-size="10.00">pipe&#45;2</text>
</g>
<g class="m-edge">
<title>p31&#45;&gt;p32</title>
<path d="M142.28,-34C144.28,-34 146.28,-34 148.29,-34"/>
<polygon points="148.24,-37.5 158.24,-34 148.24,-30.5 148.24,-37.5"/>
</g>
<g class="m-edge">
<title>p02&#45;&gt;p12</title>
<path d="M187,-253.61C187,-244.37 187,-232.73 187,-222.16"/>
<polygon points="190.5,-222.38 187,-212.38 183.5,-222.38 190.5,-222.38"/>
</g>
<g class="m-edge">
<title>p12&#45;&gt;p22</title>
<path d="M187,-174.36C187,-165.12 187,-153.48 187,-142.91"/>
<polygon points="190.5,-143.13 187,-133.13 183.5,-143.13 190.5,-143.13"/>
</g>
<g class="m-edge">
<title>p22&#45;&gt;p32</title>
<path d="M187,-95.11C187,-85.87 187,-74.23 187,-63.66"/>
<polygon points="190.5,-63.88 187,-53.88 183.5,-63.88 190.5,-63.88"/>
</g>
</g>
</svg>
</div><p>The figure above gives an example of our pipeline scheduling framework. The framework consists of three <em>pipes</em> (serial-parallel-serial stages) and four <em>lines</em> (maximum parallelism), where each line processes at most one data token. A pipeline of three pipes and four lines will propagate each data token through a sequential chain of three pipes and can simultaneously process up to four data tokens at the four lines. Each edge represents a task dependency. For example, the edge from <code>pipe-0</code> to <code>pipe-1</code> in line <code>0</code> represents the task dependency between the first and the second pipes in the first line; the edge from <code>pipe-0</code> in line <code>0</code> to <code>pipe-0</code> in line <code>1</code> represents the task dependency between two adjacent lines when processing two data tokens at the same pipe. Each pipe can be either a <em>serial</em> type or a <em>parallel</em> type, where a serial pipe processes data tokens sequentially and a parallel pipe processes different data tokens simultaneously.</p><aside class="m-note m-info"><h4>Note</h4><p>Due to the nature of pipeline, Taskflow requires the first pipe to be a serial type. The pipeline scheduling algorithm operates in a circular fashion with a factor of line count.</p></aside></section><section id="CreateATaskParallelPipelineModuleTask"><h2><a href="#CreateATaskParallelPipelineModuleTask">Create a Task-parallel Pipeline Module Task</a></h2><p>Taskflow leverages modern C++ and template techniques to strike a balance between the <em>expressiveness</em> and <em>generality</em> in designing the pipeline programming model. In general, there are three steps to create a task-parallel pipeline application:</p><ol><li>Define the pipeline structure (e.g., pipe type, pipe callable, stopping rule, line count)</li><li>Define the data storage and layout, if needed for the application</li><li>Define the pipeline taskflow graph using composition</li></ol><p>The following code creates a pipeline scheduling framework using the example from the previous section. The framework schedules a total of five <em>scheduling tokens</em> labeled from 0 to 4. The first pipe stores the token identifier in a custom data storage, <code>buffer</code>, and each of the rest pipes adds one to the input data from the result of the previous pipe and stores the result into the corresponding line entry in the buffer.</p><pre class="m-code"><span class="w"> </span><span class="mi">1</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Taskflow</span><span class="w"> </span><span class="n">taskflow</span><span class="p">;</span>
<span class="w"> </span><span class="mi">2</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Executor</span><span class="w"> </span><span class="n">executor</span><span class="p">;</span>
<span class="w"> </span><span class="mi">3</span><span class="o">:</span>
<span class="w"> </span><span class="mi">4</span><span class="o">:</span><span class="w"> </span><span class="c1">// maximum parallelism - each line processes one token at a time</span>
<span class="w"> </span><span class="mi">5</span><span class="o">:</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">size_t</span><span class="w"> </span><span class="n">num_lines</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">4</span><span class="p">;</span>
<span class="w"> </span><span class="mi">6</span><span class="o">:</span>
<span class="w"> </span><span class="mi">7</span><span class="o">:</span><span class="w"> </span><span class="c1">// custom data storage</span>
<span class="w"> </span><span class="mi">8</span><span class="o">:</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">array</span><span class="o">&lt;</span><span class="kt">int</span><span class="p">,</span><span class="w"> </span><span class="n">num_lines</span><span class="o">&gt;</span><span class="w"> </span><span class="n">buffer</span><span class="p">;</span>
<span class="w"> </span><span class="mi">9</span><span class="o">:</span>
<span class="mi">10</span><span class="o">:</span><span class="w"> </span><span class="c1">// the pipeline consists of three pipes (serial-parallel-serial)</span>
<span class="mi">11</span><span class="o">:</span><span class="w"> </span><span class="c1">// and up to four concurrent scheduling tokens</span>
<span class="mi">12</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Pipeline</span><span class="w"> </span><span class="n">pl</span><span class="p">(</span><span class="n">num_lines</span><span class="p">,</span>
<span class="mi">13</span><span class="o">:</span><span class="w">   </span><span class="n">tf</span><span class="o">::</span><span class="n">Pipe</span><span class="p">{</span><span class="n">tf</span><span class="o">::</span><span class="n">PipeType</span><span class="o">::</span><span class="n">SERIAL</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="o">&amp;</span><span class="n">buffer</span><span class="p">](</span><span class="n">tf</span><span class="o">::</span><span class="n">Pipeflow</span><span class="o">&amp;</span><span class="w"> </span><span class="n">pf</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="mi">14</span><span class="o">:</span><span class="w">     </span><span class="c1">// generate only 5 scheduling tokens</span>
<span class="mi">15</span><span class="o">:</span><span class="w">     </span><span class="k">if</span><span class="p">(</span><span class="n">pf</span><span class="p">.</span><span class="n">token</span><span class="p">()</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">5</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="mi">16</span><span class="o">:</span><span class="w">       </span><span class="n">pf</span><span class="p">.</span><span class="n">stop</span><span class="p">();</span>
<span class="mi">17</span><span class="o">:</span><span class="w">     </span><span class="p">}</span>
<span class="mi">18</span><span class="o">:</span><span class="w">     </span><span class="c1">// save the result of this pipe into the buffer</span>
<span class="mi">19</span><span class="o">:</span><span class="w">     </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="mi">20</span><span class="o">:</span><span class="w">       </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;pipe 0: input token = %zu</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">pf</span><span class="p">.</span><span class="n">token</span><span class="p">());</span>
<span class="mi">21</span><span class="o">:</span><span class="w">       </span><span class="n">buffer</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">pf</span><span class="p">.</span><span class="n">token</span><span class="p">();</span>
<span class="mi">22</span><span class="o">:</span><span class="w">     </span><span class="p">}</span>
<span class="mi">23</span><span class="o">:</span><span class="w">   </span><span class="p">}},</span>
<span class="mi">24</span><span class="o">:</span>
<span class="mi">25</span><span class="o">:</span><span class="w">   </span><span class="n">tf</span><span class="o">::</span><span class="n">Pipe</span><span class="p">{</span><span class="n">tf</span><span class="o">::</span><span class="n">PipeType</span><span class="o">::</span><span class="n">PARALLEL</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="o">&amp;</span><span class="n">buffer</span><span class="p">](</span><span class="n">tf</span><span class="o">::</span><span class="n">Pipeflow</span><span class="o">&amp;</span><span class="w"> </span><span class="n">pf</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="mi">26</span><span class="o">:</span><span class="w">     </span><span class="n">printf</span><span class="p">(</span>
<span class="mi">27</span><span class="o">:</span><span class="w">       </span><span class="s">&quot;pipe 1: input buffer[%zu] = %d</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span>
<span class="mi">28</span><span class="o">:</span><span class="w">       </span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">(),</span><span class="w"> </span><span class="n">buffer</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span>
<span class="mi">29</span><span class="o">:</span><span class="w">     </span><span class="p">);</span>
<span class="mi">30</span><span class="o">:</span><span class="w">     </span><span class="c1">// propagate the previous result to this pipe by adding one</span>
<span class="mi">31</span><span class="o">:</span><span class="w">     </span><span class="n">buffer</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<span class="mi">32</span><span class="o">:</span><span class="w">   </span><span class="p">}},</span>
<span class="mi">33</span><span class="o">:</span>
<span class="mi">34</span><span class="o">:</span><span class="w">   </span><span class="n">tf</span><span class="o">::</span><span class="n">Pipe</span><span class="p">{</span><span class="n">tf</span><span class="o">::</span><span class="n">PipeType</span><span class="o">::</span><span class="n">SERIAL</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="o">&amp;</span><span class="n">buffer</span><span class="p">](</span><span class="n">tf</span><span class="o">::</span><span class="n">Pipeflow</span><span class="o">&amp;</span><span class="w"> </span><span class="n">pf</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="mi">35</span><span class="o">:</span><span class="w">     </span><span class="n">printf</span><span class="p">(</span>
<span class="mi">36</span><span class="o">:</span><span class="w">       </span><span class="s">&quot;pipe 2: input buffer[%zu] = %d</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span>
<span class="mi">37</span><span class="o">:</span><span class="w">       </span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">(),</span><span class="w"> </span><span class="n">buffer</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span>
<span class="mi">38</span><span class="o">:</span><span class="w">     </span><span class="p">);</span>
<span class="mi">39</span><span class="o">:</span><span class="w">     </span><span class="c1">// propagate the previous result to this pipe by adding one</span>
<span class="mi">40</span><span class="o">:</span><span class="w">     </span><span class="n">buffer</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<span class="mi">41</span><span class="o">:</span><span class="w">   </span><span class="p">}}</span>
<span class="mi">42</span><span class="o">:</span><span class="w"> </span><span class="p">);</span>
<span class="mi">43</span><span class="o">:</span>
<span class="mi">44</span><span class="o">:</span><span class="w"> </span><span class="c1">// build the pipeline graph using composition</span>
<span class="mi">45</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">pipeline</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">composed_of</span><span class="p">(</span><span class="n">pl</span><span class="p">).</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;pipeline&quot;</span><span class="p">);</span>
<span class="mi">46</span><span class="o">:</span>
<span class="mi">47</span><span class="o">:</span><span class="w"> </span><span class="c1">// execute the taskflow</span>
<span class="mi">48</span><span class="o">:</span><span class="w"> </span><span class="n">executor</span><span class="p">.</span><span class="n">run</span><span class="p">(</span><span class="n">taskflow</span><span class="p">).</span><span class="n">wait</span><span class="p">();</span></pre><p>Debrief:</p><ul><li>Lines 4-5 define the structure of the pipeline scheduling framework</li><li>Line 8 defines the data storage as an one-dimensional array of <code>num_lines</code> integers</li><li>Line 12 defines the number of lines in the pipeline</li><li>Lines 13-23 define the first serial pipe, which will stop the pipeline scheduling at the fifth token</li><li>Lines 25-32 define the second parallel pipe</li><li>Lines 34-41 define the third serial pipe</li><li>Line 45 defines the pipeline taskflow graph using composition</li><li>Line 48 executes the taskflow</li></ul><p>Taskflow leverages <a href="RuntimeTasking.html" class="m-doc">Interact with the Runtime</a> and <a href="ComposableTasking.html" class="m-doc">Composable Tasking</a> to implement the pipeline scheduling framework. The taskflow graph of this pipeline example is shown as follows, where 1) one condition task is used to decide which runtime task to run and 2) four runtime tasks is used to schedule tokens at four parallel lines, respectively.</p><div class="m-graph"><svg style="width: 40.600rem; height: 17.900rem;" viewBox="0.00 0.00 406.00 178.50">
<g transform="scale(1 1) rotate(0) translate(4 174.5)">
<title>Taskflow</title>
<g class="m-cluster">
<title>cluster_p0x7ffc47e53358</title>
<polygon points="8,-91.25 8,-162.5 96,-162.5 96,-91.25 8,-91.25"/>
<text text-anchor="middle" x="52" y="-149" font-family="Helvetica,sans-Serif" font-size="10.00">Taskflow</text>
</g>
<g class="m-cluster">
<title>cluster_p0x7ffc47e53220</title>
<polygon points="104,-8 104,-162.5 390,-162.5 390,-8 104,-8"/>
<text text-anchor="middle" x="247" y="-149" font-family="Helvetica,sans-Serif" font-size="10.00">m1</text>
</g>
<g class="m-node m-flat">
<title>p0x1878a88</title>
<polygon points="87.75,-135.25 20.25,-135.25 16.25,-131.25 16.25,-99.25 83.75,-99.25 87.75,-103.25 87.75,-135.25"/>
<polyline points="83.75,-131.25 16.25,-131.25"/>
<polyline points="83.75,-131.25 83.75,-99.25"/>
<polyline points="83.75,-131.25 87.75,-135.25"/>
<text text-anchor="middle" x="52" y="-113.38" font-family="Helvetica,sans-Serif" font-size="10.00">pipeline [m1]</text>
</g>
<g class="m-node">
<title>p0x1878600</title>
<polygon points="247,-135.25 216.04,-117.25 247,-99.25 277.96,-117.25 247,-135.25"/>
<text text-anchor="middle" x="247" y="-113.38" font-family="Helvetica,sans-Serif" font-size="10.00">cond</text>
</g>
<g class="m-node m-flat">
<title>p0x18786e8</title>
<polygon points="166,-52 112,-52 112,-48 108,-48 108,-44 112,-44 112,-24 108,-24 108,-20 112,-20 112,-16 166,-16 166,-52"/>
<polyline points="112,-48 116,-48 116,-44 112,-44"/>
<polyline points="112,-24 116,-24 116,-20 112,-20"/>
<text text-anchor="middle" x="139" y="-30.12" font-family="Helvetica,sans-Serif" font-size="10.00">rt&#45;0</text>
</g>
<g class="m-edge">
<title>p0x1878600&#45;&gt;p0x18786e8</title>
<path stroke-dasharray="5,2" d="M234.19,-106.61C218.71,-94.97 192.12,-74.96 170.97,-59.05"/>
<polygon points="173.29,-56.42 163.2,-53.2 169.09,-62.01 173.29,-56.42"/>
<text text-anchor="middle" x="201.62" y="-71.75" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-node m-flat">
<title>p0x18787d0</title>
<polygon points="238,-52 184,-52 184,-48 180,-48 180,-44 184,-44 184,-24 180,-24 180,-20 184,-20 184,-16 238,-16 238,-52"/>
<polyline points="184,-48 188,-48 188,-44 184,-44"/>
<polyline points="184,-24 188,-24 188,-20 184,-20"/>
<text text-anchor="middle" x="211" y="-30.12" font-family="Helvetica,sans-Serif" font-size="10.00">rt&#45;1</text>
</g>
<g class="m-edge">
<title>p0x1878600&#45;&gt;p0x18787d0</title>
<path stroke-dasharray="5,2" d="M240.89,-102.47C236.05,-91.54 229.13,-75.91 223.16,-62.45"/>
<polygon points="226.51,-61.36 219.26,-53.63 220.11,-64.19 226.51,-61.36"/>
<text text-anchor="middle" x="233.62" y="-71.75" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
<g class="m-node m-flat">
<title>p0x18788b8</title>
<polygon points="310,-52 256,-52 256,-48 252,-48 252,-44 256,-44 256,-24 252,-24 252,-20 256,-20 256,-16 310,-16 310,-52"/>
<polyline points="256,-48 260,-48 260,-44 256,-44"/>
<polyline points="256,-24 260,-24 260,-20 256,-20"/>
<text text-anchor="middle" x="283" y="-30.12" font-family="Helvetica,sans-Serif" font-size="10.00">rt&#45;2</text>
</g>
<g class="m-edge">
<title>p0x1878600&#45;&gt;p0x18788b8</title>
<path stroke-dasharray="5,2" d="M253.11,-102.47C257.95,-91.54 264.87,-75.91 270.84,-62.45"/>
<polygon points="273.89,-64.19 274.74,-53.63 267.49,-61.36 273.89,-64.19"/>
<text text-anchor="middle" x="269.62" y="-71.75" font-family="Helvetica,sans-Serif" font-size="10.00">2</text>
</g>
<g class="m-node m-flat">
<title>p0x18789a0</title>
<polygon points="382,-52 328,-52 328,-48 324,-48 324,-44 328,-44 328,-24 324,-24 324,-20 328,-20 328,-16 382,-16 382,-52"/>
<polyline points="328,-48 332,-48 332,-44 328,-44"/>
<polyline points="328,-24 332,-24 332,-20 328,-20"/>
<text text-anchor="middle" x="355" y="-30.12" font-family="Helvetica,sans-Serif" font-size="10.00">rt&#45;3</text>
</g>
<g class="m-edge">
<title>p0x1878600&#45;&gt;p0x18789a0</title>
<path stroke-dasharray="5,2" d="M259.81,-106.61C275.29,-94.97 301.88,-74.96 323.03,-59.05"/>
<polygon points="324.91,-62.01 330.8,-53.2 320.71,-56.42 324.91,-62.01"/>
<text text-anchor="middle" x="309.62" y="-71.75" font-family="Helvetica,sans-Serif" font-size="10.00">3</text>
</g>
</g>
</svg>
</div><p>In this example, we customize the data storage, <code>buffer</code>, as an one-dimensional array of 4 integers, since the pipeline structure defines only four parallel lines. Each entry of <code>buffer</code> stores stores the data being processed in the corresponding line. For example, <code>buffer[1]</code> stores the processed data at line <code>1</code>. The following figure shows the data layout of <code>buffer</code>.</p><div class="m-graph"><svg style="width: 17.900rem; height: 8.600rem;" viewBox="0.00 0.00 178.50 86.00">
<g transform="scale(1 1) rotate(0) translate(4 82)">
<title>structs</title>
<g class="m-node m-flat">
<title>struct1</title>
<polygon points="0,-0.5 0,-77.5 170.5,-77.5 170.5,-0.5 0,-0.5"/>
<text text-anchor="middle" x="85.25" y="-64" font-family="Helvetica,sans-Serif" font-size="10.00">buffer[0] (processes token at line 0)</text>
<polyline points="0,-58.25 170.5,-58.25"/>
<text text-anchor="middle" x="85.25" y="-44.75" font-family="Helvetica,sans-Serif" font-size="10.00">buffer[1] (processes token at line 1)</text>
<polyline points="0,-39 170.5,-39"/>
<text text-anchor="middle" x="85.25" y="-25.5" font-family="Helvetica,sans-Serif" font-size="10.00">buffer[2] (processes token at line 2)</text>
<polyline points="0,-19.75 170.5,-19.75"/>
<text text-anchor="middle" x="85.25" y="-6.25" font-family="Helvetica,sans-Serif" font-size="10.00">buffer[3] (processes token at line 3)</text>
</g>
</g>
</svg>
</div><aside class="m-note m-info"><h4>Note</h4><p>In practice, you may need to add padding to the data type of the buffer or align it with the cacheline size to avoid false sharing. If the data type varies at different pipes, you can use <a href="https://en.cppreference.com/w/cpp/utility/variant">std::<wbr />variant</a> to store the data types in a uniform storage.</p></aside><p>For each scheduling token, you can use <a href="classtf_1_1Pipeflow.html#afee054e6a99965d4b3e36ff903227e6c" class="m-doc">tf::<wbr />Pipeflow::<wbr />line()</a> to get its line identifier and <a href="classtf_1_1Pipeflow.html#a4914c1f381a3016e98285b019cf60d6d" class="m-doc">tf::<wbr />Pipeflow::<wbr />pipe()</a> to get its pipe identifier. For example, if a scheduling token is at the third pipe of the forth line, <a href="classtf_1_1Pipeflow.html#afee054e6a99965d4b3e36ff903227e6c" class="m-doc">tf::<wbr />Pipeflow::<wbr />line()</a> will return <code>3</code> and <a href="classtf_1_1Pipeflow.html#a4914c1f381a3016e98285b019cf60d6d" class="m-doc">tf::<wbr />Pipeflow::<wbr />pipe()</a> will return <code>2</code> (index starts from 0). To stop the execution of the pipeline, you need to call <a href="classtf_1_1Pipeflow.html#a830b7f204cb87fff17e8d424918d9453" class="m-doc">tf::<wbr />Pipeflow::<wbr />stop()</a> at the first pipe. Once the stop signal has been triggered, the pipeline will stop scheduling any new tokens after the callable. As we can see from this example, <a href="classtf_1_1Pipeline.html" class="m-doc">tf::<wbr />Pipeline</a> gives you the full control to customize your application data on top of a pipeline scheduling framework.</p><aside class="m-note m-info"><h4>Note</h4><ol><li>Calling <a href="classtf_1_1Pipeflow.html#a830b7f204cb87fff17e8d424918d9453" class="m-doc">tf::<wbr />Pipeflow::<wbr />stop()</a> not at the first pipe has no effect on the pipeline scheduling.</li><li>In most cases, <a href="http://en.cppreference.com/w/cpp/thread/thread/hardware_concurrency.html" class="m-doc-external">std::<wbr />thread::<wbr />hardware_concurrency</a> is a good number for line count.</li></ol></aside><p>Our pipeline algorithm schedules tokens in a <em>circular</em> manner, with a factor of <code>num_lines</code>. That is, token <code>t</code> will be processed at line <code>t % num_lines</code>. The following snippet shows one of the possible outputs of this pipeline program:</p><pre class="m-code">pipe<span class="w"> </span><span class="m">0</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span>
pipe<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span>buffer<span class="o">[</span><span class="m">0</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span>
pipe<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer<span class="o">[</span><span class="m">0</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span>
pipe<span class="w"> </span><span class="m">0</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span>
pipe<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span>buffer<span class="o">[</span><span class="m">1</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span>
pipe<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer<span class="o">[</span><span class="m">1</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">2</span>
pipe<span class="w"> </span><span class="m">0</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">2</span>
pipe<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span>buffer<span class="o">[</span><span class="m">2</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">2</span>
pipe<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer<span class="o">[</span><span class="m">2</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">3</span>
pipe<span class="w"> </span><span class="m">0</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">3</span>
pipe<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span>buffer<span class="o">[</span><span class="m">3</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">3</span>
pipe<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer<span class="o">[</span><span class="m">3</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">4</span>
pipe<span class="w"> </span><span class="m">0</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">4</span>
pipe<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span>buffer<span class="o">[</span><span class="m">0</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">4</span>
pipe<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer<span class="o">[</span><span class="m">0</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">5</span></pre><p>There are a total of five tokens running through three pipes. Each pipes prints its input data value, except the first pipe that prints its token identifier. Since the second pipe is a parallel pipe, the output can interleave.</p></section><section id="ConnectWithTasks"><h2><a href="#ConnectWithTasks">Connect Pipeline with Other Tasks</a></h2><p>You can connect the pipeline module task with other tasks to create a taskflow application that embeds one or multiple pipeline algorithms. We describe three common examples below:</p><ul><li><a href="TaskParallelPipeline.html#IterateAPipeline" class="m-doc">Example 1: Iterate a Pipeline</a></li><li><a href="TaskParallelPipeline.html#ConcatenateTwoPipelines" class="m-doc">Example 2: Concatenate Two Pipelines</a></li><li><a href="TaskParallelPipeline.html#DefineMultipleTaskParallelPipelines" class="m-doc">Example 3: Define Multiple Parallel Pipelines</a></li></ul><section id="IterateAPipeline"><h3><a href="#IterateAPipeline">Example 1: Iterate a Pipeline</a></h3><p>This example emulates a data streaming application that iteratively runs a stream of data through a pipeline using conditional tasking. The taskflow graph consists of one pipeline module task and one condition task. The pipeline module task processes a stream of data. The condition task decides the availability of data and reruns the pipeline when the next stream of data becomes available.<br /></p><pre class="m-code"><span class="w"> </span><span class="mi">1</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Taskflow</span><span class="w"> </span><span class="n">taskflow</span><span class="p">;</span>
<span class="w"> </span><span class="mi">2</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Executor</span><span class="w"> </span><span class="n">executor</span><span class="p">;</span>
<span class="w"> </span><span class="mi">3</span><span class="o">:</span>
<span class="w"> </span><span class="mi">4</span><span class="o">:</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">size_t</span><span class="w"> </span><span class="n">num_lines</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">4</span><span class="p">;</span><span class="w">  </span><span class="c1">// maximum parallelism of the pipeline</span>
<span class="w"> </span><span class="mi">5</span><span class="o">:</span><span class="w"> </span>
<span class="w"> </span><span class="mi">6</span><span class="o">:</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">N</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w"> </span><span class="mi">7</span><span class="o">:</span><span class="w"> </span><span class="c1">// custom data storage</span>
<span class="w"> </span><span class="mi">8</span><span class="o">:</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">array</span><span class="o">&lt;</span><span class="kt">int</span><span class="p">,</span><span class="w"> </span><span class="n">num_lines</span><span class="o">&gt;</span><span class="w"> </span><span class="n">buffer</span><span class="p">;</span>
<span class="w"> </span><span class="mi">9</span><span class="o">:</span>
<span class="mi">10</span><span class="o">:</span><span class="w"> </span><span class="c1">// the pipeline consists of three pipes (serial-parallel-serial)</span>
<span class="mi">11</span><span class="o">:</span><span class="w"> </span><span class="c1">// and up to four concurrent scheduling tokens</span>
<span class="mi">12</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Pipeline</span><span class="w"> </span><span class="n">pl</span><span class="p">(</span><span class="n">num_lines</span><span class="p">,</span>
<span class="mi">13</span><span class="o">:</span><span class="w">   </span><span class="n">tf</span><span class="o">::</span><span class="n">Pipe</span><span class="p">{</span><span class="n">tf</span><span class="o">::</span><span class="n">PipeType</span><span class="o">::</span><span class="n">SERIAL</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="o">&amp;</span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">buffer</span><span class="p">](</span><span class="n">tf</span><span class="o">::</span><span class="n">Pipeflow</span><span class="o">&amp;</span><span class="w"> </span><span class="n">pf</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="mi">14</span><span class="o">:</span><span class="w">     </span><span class="c1">// only 5 scheduling tokens are processed</span>
<span class="mi">15</span><span class="o">:</span><span class="w">     </span><span class="k">if</span><span class="p">(</span><span class="n">i</span><span class="o">++</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">5</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="mi">16</span><span class="o">:</span><span class="w">       </span><span class="n">pf</span><span class="p">.</span><span class="n">stop</span><span class="p">();</span>
<span class="mi">17</span><span class="o">:</span><span class="w">     </span><span class="p">}</span>
<span class="mi">18</span><span class="o">:</span><span class="w">     </span><span class="c1">// save the result of this pipe into the buffer</span>
<span class="mi">19</span><span class="o">:</span><span class="w">     </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="mi">20</span><span class="o">:</span><span class="w">       </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;stage 0: input token = %zu</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">pf</span><span class="p">.</span><span class="n">token</span><span class="p">());</span>
<span class="mi">21</span><span class="o">:</span><span class="w">       </span><span class="n">buffer</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">pf</span><span class="p">.</span><span class="n">token</span><span class="p">();</span>
<span class="mi">22</span><span class="o">:</span><span class="w">     </span><span class="p">}</span>
<span class="mi">23</span><span class="o">:</span><span class="w">   </span><span class="p">}},</span>
<span class="mi">24</span><span class="o">:</span>
<span class="mi">25</span><span class="o">:</span><span class="w">   </span><span class="n">tf</span><span class="o">::</span><span class="n">Pipe</span><span class="p">{</span><span class="n">tf</span><span class="o">::</span><span class="n">PipeType</span><span class="o">::</span><span class="n">PARALLEL</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="o">&amp;</span><span class="n">buffer</span><span class="p">](</span><span class="n">tf</span><span class="o">::</span><span class="n">Pipeflow</span><span class="o">&amp;</span><span class="w"> </span><span class="n">pf</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="mi">26</span><span class="o">:</span><span class="w">     </span><span class="n">printf</span><span class="p">(</span>
<span class="mi">27</span><span class="o">:</span><span class="w">       </span><span class="s">&quot;stage 1: input buffer[%zu] = %d</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span>
<span class="mi">28</span><span class="o">:</span><span class="w">       </span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">(),</span><span class="w"> </span><span class="n">buffer</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span>
<span class="mi">29</span><span class="o">:</span><span class="w">     </span><span class="p">);</span>
<span class="mi">30</span><span class="o">:</span><span class="w">     </span><span class="c1">// propagate the previous result to this pipe by adding one</span>
<span class="mi">31</span><span class="o">:</span><span class="w">     </span><span class="n">buffer</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<span class="mi">32</span><span class="o">:</span><span class="w">   </span><span class="p">}},</span>
<span class="mi">33</span><span class="o">:</span>
<span class="mi">34</span><span class="o">:</span><span class="w">   </span><span class="n">tf</span><span class="o">::</span><span class="n">Pipe</span><span class="p">{</span><span class="n">tf</span><span class="o">::</span><span class="n">PipeType</span><span class="o">::</span><span class="n">SERIAL</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="o">&amp;</span><span class="n">buffer</span><span class="p">](</span><span class="n">tf</span><span class="o">::</span><span class="n">Pipeflow</span><span class="o">&amp;</span><span class="w"> </span><span class="n">pf</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="mi">35</span><span class="o">:</span><span class="w">     </span><span class="n">printf</span><span class="p">(</span>
<span class="mi">36</span><span class="o">:</span><span class="w">       </span><span class="s">&quot;stage 2: input buffer[%zu] = %d</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span>
<span class="mi">37</span><span class="o">:</span><span class="w">       </span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">(),</span><span class="w"> </span><span class="n">buffer</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span>
<span class="mi">38</span><span class="o">:</span><span class="w">     </span><span class="p">);</span>
<span class="mi">39</span><span class="o">:</span><span class="w">     </span><span class="c1">// propagate the previous result to this pipe by adding one</span>
<span class="mi">40</span><span class="o">:</span><span class="w">     </span><span class="n">buffer</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<span class="mi">41</span><span class="o">:</span><span class="w">   </span><span class="p">}}</span>
<span class="mi">42</span><span class="o">:</span><span class="w"> </span><span class="p">);</span>
<span class="mi">43</span><span class="o">:</span><span class="w"> </span>
<span class="mi">44</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">conditional</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&amp;</span><span class="n">N</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">i</span><span class="p">](){</span>
<span class="mi">45</span><span class="o">:</span><span class="w">   </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="mi">46</span><span class="o">:</span><span class="w">   </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="o">++</span><span class="n">N</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="mi">2</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">  </span>
<span class="mi">47</span><span class="o">:</span><span class="w">     </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;Rerun the pipeline</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span>
<span class="mi">48</span><span class="o">:</span><span class="w">     </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="mi">49</span><span class="o">:</span><span class="w">   </span><span class="p">}</span>
<span class="mi">50</span><span class="o">:</span><span class="w">   </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="mi">51</span><span class="o">:</span><span class="w">     </span><span class="k">return</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<span class="mi">52</span><span class="o">:</span><span class="w">   </span><span class="p">}</span>
<span class="mi">53</span><span class="o">:</span><span class="w"> </span><span class="p">}).</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;conditional&quot;</span><span class="p">);</span>
<span class="mi">54</span><span class="o">:</span>
<span class="mi">55</span><span class="o">:</span><span class="w"> </span><span class="c1">// build the pipeline graph using composition</span>
<span class="mi">56</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">pipeline</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">composed_of</span><span class="p">(</span><span class="n">pl</span><span class="p">)</span>
<span class="mi">57</span><span class="o">:</span><span class="w">                             </span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;pipeline&quot;</span><span class="p">);</span>
<span class="mi">58</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">initial</span><span class="w">  </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;initial</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w">  </span><span class="p">})</span>
<span class="mi">59</span><span class="o">:</span><span class="w">                             </span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;initial&quot;</span><span class="p">);</span>
<span class="mi">60</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">stop</span><span class="w">     </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;stop</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w"> </span><span class="p">})</span>
<span class="mi">61</span><span class="o">:</span><span class="w">                             </span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;stop&quot;</span><span class="p">);</span>
<span class="mi">62</span><span class="o">:</span>
<span class="mi">63</span><span class="o">:</span><span class="w"> </span><span class="c1">// specify the graph dependency</span>
<span class="mi">64</span><span class="o">:</span><span class="w"> </span><span class="n">initial</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">pipeline</span><span class="p">);</span>
<span class="mi">65</span><span class="o">:</span><span class="w"> </span><span class="n">pipeline</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">conditional</span><span class="p">);</span>
<span class="mi">66</span><span class="o">:</span><span class="w"> </span><span class="n">conditional</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">pipeline</span><span class="p">,</span><span class="w"> </span><span class="n">stop</span><span class="p">);</span>
<span class="mi">67</span><span class="o">:</span>
<span class="mi">68</span><span class="o">:</span><span class="w"> </span><span class="c1">// execute the taskflow</span>
<span class="mi">69</span><span class="o">:</span><span class="w"> </span><span class="n">executor</span><span class="p">.</span><span class="n">run</span><span class="p">(</span><span class="n">taskflow</span><span class="p">).</span><span class="n">wait</span><span class="p">();</span></pre><p>Debrief:</p><ul><li>Lines 4-5 define the structure of the pipeline scheduling framework</li><li>Line 8 defines the data storage as an one-dimensional array (<code>num_lines</code> integers)</li><li>Line 12 defines the number of lines in the pipeline</li><li>Lines 13-23 define the first serial pipe, which will stop the pipeline scheduling when <code>i</code> is <code>5</code><br /></li><li>Lines 25-32 define the second parallel pipe</li><li>Lines 34-41 define the third serial pipe</li><li>Lines 44-53 define a condition task which returns 0 when <code>N</code> is less than <code>2</code>, otherwise returns <code>1</code></li><li>Line 45 resets variable <code>i</code></li><li>Lines 56-57 define the pipeline graph using composition</li><li>Lines 58-61 define two static tasks</li><li>Line 64-66 define the task dependency</li><li>Line 69 executes the taskflow</li></ul><p>The taskflow graph of this pipeline example is illustrated as follows:</p><div class="m-graph"><svg style="width: 43.800rem; height: 34.500rem;" viewBox="0.00 0.00 438.00 345.00">
<g transform="scale(1 1) rotate(0) translate(4 341)">
<title>Taskflow</title>
<g class="m-cluster">
<title>cluster_p0x7fff4f1276d8</title>
<polygon points="8,-8 8,-329 128,-329 128,-8 8,-8"/>
<text text-anchor="middle" x="68" y="-315.5" font-family="Helvetica,sans-Serif" font-size="10.00">Taskflow</text>
</g>
<g class="m-cluster">
<title>cluster_p0x7fff4f127590</title>
<polygon points="136,-174.5 136,-329 422,-329 422,-174.5 136,-174.5"/>
<text text-anchor="middle" x="279" y="-315.5" font-family="Helvetica,sans-Serif" font-size="10.00">m1</text>
</g>
<g class="m-node m-flat">
<title>p0xdf4c58</title>
<ellipse cx="60" cy="-283.75" rx="27" ry="18"/>
<text text-anchor="middle" x="60" y="-279.88" font-family="Helvetica,sans-Serif" font-size="10.00">initial</text>
</g>
<g class="m-node m-flat">
<title>p0xdf4b70</title>
<polygon points="95.75,-218.5 28.25,-218.5 24.25,-214.5 24.25,-182.5 91.75,-182.5 95.75,-186.5 95.75,-218.5"/>
<polyline points="91.75,-214.5 24.25,-214.5"/>
<polyline points="91.75,-214.5 91.75,-182.5"/>
<polyline points="91.75,-214.5 95.75,-218.5"/>
<text text-anchor="middle" x="60" y="-196.62" font-family="Helvetica,sans-Serif" font-size="10.00">pipeline [m1]</text>
</g>
<g class="m-edge">
<title>p0xdf4c58&#45;&gt;p0xdf4b70</title>
<path d="M60,-265.52C60,-255.25 60,-241.95 60,-230.14"/>
<polygon points="63.5,-230.5 60,-220.5 56.5,-230.5 63.5,-230.5"/>
</g>
<g class="m-node">
<title>p0xdf4a88</title>
<polygon points="68,-135.25 15.7,-117.25 68,-99.25 120.3,-117.25 68,-135.25"/>
<text text-anchor="middle" x="68" y="-113.38" font-family="Helvetica,sans-Serif" font-size="10.00">conditional</text>
</g>
<g class="m-edge">
<title>p0xdf4b70&#45;&gt;p0xdf4a88</title>
<path d="M57.12,-182.29C56.1,-173.55 55.54,-162.79 57,-153.25 57.44,-150.39 58.07,-147.43 58.81,-144.52"/>
<polygon points="62.1,-145.72 61.6,-135.14 55.39,-143.73 62.1,-145.72"/>
</g>
<g class="m-edge">
<title>p0xdf4a88&#45;&gt;p0xdf4b70</title>
<path stroke-dasharray="5,2" d="M66.33,-135.18C65.31,-145.53 63.98,-159.03 62.81,-171"/>
<polygon points="59.34,-170.47 61.84,-180.76 66.31,-171.15 59.34,-170.47"/>
<text text-anchor="middle" x="66.62" y="-155" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-node m-flat">
<title>p0xdf4d40</title>
<ellipse cx="68" cy="-34" rx="29.88" ry="18"/>
<text text-anchor="middle" x="68" y="-30.12" font-family="Helvetica,sans-Serif" font-size="10.00">terminal</text>
</g>
<g class="m-edge">
<title>p0xdf4a88&#45;&gt;p0xdf4d40</title>
<path stroke-dasharray="5,2" d="M68,-99.02C68,-88.75 68,-75.45 68,-63.64"/>
<polygon points="71.5,-64 68,-54 64.5,-64 71.5,-64"/>
<text text-anchor="middle" x="70.62" y="-71.75" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
<g class="m-node">
<title>p0xdf4600</title>
<polygon points="279,-301.75 248.04,-283.75 279,-265.75 309.96,-283.75 279,-301.75"/>
<text text-anchor="middle" x="279" y="-279.88" font-family="Helvetica,sans-Serif" font-size="10.00">cond</text>
</g>
<g class="m-node m-flat">
<title>p0xdf46e8</title>
<polygon points="198,-218.5 144,-218.5 144,-214.5 140,-214.5 140,-210.5 144,-210.5 144,-190.5 140,-190.5 140,-186.5 144,-186.5 144,-182.5 198,-182.5 198,-218.5"/>
<polyline points="144,-214.5 148,-214.5 148,-210.5 144,-210.5"/>
<polyline points="144,-190.5 148,-190.5 148,-186.5 144,-186.5"/>
<text text-anchor="middle" x="171" y="-196.62" font-family="Helvetica,sans-Serif" font-size="10.00">rt&#45;0</text>
</g>
<g class="m-edge">
<title>p0xdf4600&#45;&gt;p0xdf46e8</title>
<path stroke-dasharray="5,2" d="M266.19,-273.11C250.71,-261.47 224.12,-241.46 202.97,-225.55"/>
<polygon points="205.29,-222.92 195.2,-219.7 201.09,-228.51 205.29,-222.92"/>
<text text-anchor="middle" x="233.62" y="-238.25" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-node m-flat">
<title>p0xdf47d0</title>
<polygon points="270,-218.5 216,-218.5 216,-214.5 212,-214.5 212,-210.5 216,-210.5 216,-190.5 212,-190.5 212,-186.5 216,-186.5 216,-182.5 270,-182.5 270,-218.5"/>
<polyline points="216,-214.5 220,-214.5 220,-210.5 216,-210.5"/>
<polyline points="216,-190.5 220,-190.5 220,-186.5 216,-186.5"/>
<text text-anchor="middle" x="243" y="-196.62" font-family="Helvetica,sans-Serif" font-size="10.00">rt&#45;1</text>
</g>
<g class="m-edge">
<title>p0xdf4600&#45;&gt;p0xdf47d0</title>
<path stroke-dasharray="5,2" d="M272.89,-268.97C268.05,-258.04 261.13,-242.41 255.16,-228.95"/>
<polygon points="258.51,-227.86 251.26,-220.13 252.11,-230.69 258.51,-227.86"/>
<text text-anchor="middle" x="265.62" y="-238.25" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
<g class="m-node m-flat">
<title>p0xdf48b8</title>
<polygon points="342,-218.5 288,-218.5 288,-214.5 284,-214.5 284,-210.5 288,-210.5 288,-190.5 284,-190.5 284,-186.5 288,-186.5 288,-182.5 342,-182.5 342,-218.5"/>
<polyline points="288,-214.5 292,-214.5 292,-210.5 288,-210.5"/>
<polyline points="288,-190.5 292,-190.5 292,-186.5 288,-186.5"/>
<text text-anchor="middle" x="315" y="-196.62" font-family="Helvetica,sans-Serif" font-size="10.00">rt&#45;2</text>
</g>
<g class="m-edge">
<title>p0xdf4600&#45;&gt;p0xdf48b8</title>
<path stroke-dasharray="5,2" d="M285.11,-268.97C289.95,-258.04 296.87,-242.41 302.84,-228.95"/>
<polygon points="305.89,-230.69 306.74,-220.13 299.49,-227.86 305.89,-230.69"/>
<text text-anchor="middle" x="301.62" y="-238.25" font-family="Helvetica,sans-Serif" font-size="10.00">2</text>
</g>
<g class="m-node m-flat">
<title>p0xdf49a0</title>
<polygon points="414,-218.5 360,-218.5 360,-214.5 356,-214.5 356,-210.5 360,-210.5 360,-190.5 356,-190.5 356,-186.5 360,-186.5 360,-182.5 414,-182.5 414,-218.5"/>
<polyline points="360,-214.5 364,-214.5 364,-210.5 360,-210.5"/>
<polyline points="360,-190.5 364,-190.5 364,-186.5 360,-186.5"/>
<text text-anchor="middle" x="387" y="-196.62" font-family="Helvetica,sans-Serif" font-size="10.00">rt&#45;3</text>
</g>
<g class="m-edge">
<title>p0xdf4600&#45;&gt;p0xdf49a0</title>
<path stroke-dasharray="5,2" d="M291.81,-273.11C307.29,-261.47 333.88,-241.46 355.03,-225.55"/>
<polygon points="356.91,-228.51 362.8,-219.7 352.71,-222.92 356.91,-228.51"/>
<text text-anchor="middle" x="341.62" y="-238.25" font-family="Helvetica,sans-Serif" font-size="10.00">3</text>
</g>
</g>
</svg>
</div><p>The following snippet shows one of the possible outputs:</p><pre class="m-code">initial
stage<span class="w"> </span><span class="m">0</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span>
stage<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span>buffer<span class="o">[</span><span class="m">0</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span>
stage<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer<span class="o">[</span><span class="m">0</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span>
stage<span class="w"> </span><span class="m">0</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span>
stage<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span>buffer<span class="o">[</span><span class="m">1</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span>
stage<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer<span class="o">[</span><span class="m">1</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">2</span>
stage<span class="w"> </span><span class="m">0</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">2</span>
stage<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span>buffer<span class="o">[</span><span class="m">2</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">2</span>
stage<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer<span class="o">[</span><span class="m">2</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">3</span>
stage<span class="w"> </span><span class="m">0</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">3</span>
stage<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span>buffer<span class="o">[</span><span class="m">3</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">3</span>
stage<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer<span class="o">[</span><span class="m">3</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">4</span>
stage<span class="w"> </span><span class="m">0</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">4</span>
stage<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span>buffer<span class="o">[</span><span class="m">0</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">4</span>
stage<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer<span class="o">[</span><span class="m">0</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">5</span>
Rerun<span class="w"> </span>the<span class="w"> </span>pipeline
stage<span class="w"> </span><span class="m">0</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">5</span>
stage<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span>buffer<span class="o">[</span><span class="m">1</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">5</span>
stage<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer<span class="o">[</span><span class="m">1</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">6</span>
stage<span class="w"> </span><span class="m">0</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">6</span>
stage<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span>buffer<span class="o">[</span><span class="m">2</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">6</span>
stage<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer<span class="o">[</span><span class="m">2</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">7</span>
stage<span class="w"> </span><span class="m">0</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">7</span>
stage<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span>buffer<span class="o">[</span><span class="m">3</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">7</span>
stage<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer<span class="o">[</span><span class="m">3</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">8</span>
stage<span class="w"> </span><span class="m">0</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">8</span>
stage<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span>buffer<span class="o">[</span><span class="m">0</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">8</span>
stage<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer<span class="o">[</span><span class="m">0</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">9</span>
stage<span class="w"> </span><span class="m">0</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">9</span>
stage<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span>buffer<span class="o">[</span><span class="m">1</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">9</span>
stage<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer<span class="o">[</span><span class="m">1</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">10</span>
stop</pre><p>The pipeline runs twice as controlled by the condition task <code>conditional</code>. The starting token in the second run of the pipeline is <code>5</code> rather than <code>0</code> because the pipeline keeps a stateful number of tokens. The last token is <code>9</code>, which means the pipeline processes in total <code>10</code> scheduling tokens. The first five tokens (token <code>0</code> to <code>4</code>) are processed in the first run, and the remaining five tokens (token <code>5</code> to <code>9</code>) are processed in the second run. In the condition task, we use <code>N</code> as a decision-making counter to process the next stream of data.</p></section><section id="ConcatenateTwoPipelines"><h3><a href="#ConcatenateTwoPipelines">Example 2: Concatenate Two Pipelines</a></h3><p>This example demonstrates two concatenated pipelines where a sequence of data tokens run synchronously from one pipeline to another pipeline. The first pipeline task precedes the second pipeline task.</p><pre class="m-code"><span class="w"> </span><span class="mi">1</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Taskflow</span><span class="w"> </span><span class="n">taskflow</span><span class="p">(</span><span class="s">&quot;pipeline&quot;</span><span class="p">);</span>
<span class="w"> </span><span class="mi">2</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Executor</span><span class="w"> </span><span class="n">executor</span><span class="p">;</span>
<span class="w"> </span><span class="mi">3</span><span class="o">:</span>
<span class="w"> </span><span class="mi">4</span><span class="o">:</span><span class="w"> </span><span class="c1">// define the maximum parallelism of the pipeline</span>
<span class="w"> </span><span class="mi">5</span><span class="o">:</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">size_t</span><span class="w"> </span><span class="n">num_lines</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">4</span><span class="p">;</span>
<span class="w"> </span><span class="mi">6</span><span class="o">:</span>
<span class="w"> </span><span class="mi">7</span><span class="o">:</span><span class="w"> </span><span class="c1">// custom data storage</span>
<span class="w"> </span><span class="mi">8</span><span class="o">:</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">array</span><span class="o">&lt;</span><span class="kt">int</span><span class="p">,</span><span class="w"> </span><span class="n">num_lines</span><span class="o">&gt;</span><span class="w"> </span><span class="n">buffer_1</span><span class="p">;</span>
<span class="w"> </span><span class="mi">9</span><span class="o">:</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">array</span><span class="o">&lt;</span><span class="kt">int</span><span class="p">,</span><span class="w"> </span><span class="n">num_lines</span><span class="o">&gt;</span><span class="w"> </span><span class="n">buffer_2</span><span class="p">;</span>
<span class="mi">10</span><span class="o">:</span><span class="w"> </span>
<span class="mi">11</span><span class="o">:</span><span class="w"> </span><span class="c1">// the pipeline_1 consists of three pipes (serial-parallel-serial)</span>
<span class="mi">12</span><span class="o">:</span><span class="w"> </span><span class="c1">// and up to four concurrent scheduling tokens</span>
<span class="mi">13</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Pipeline</span><span class="w"> </span><span class="n">pl_1</span><span class="p">(</span><span class="n">num_lines</span><span class="p">,</span>
<span class="mi">14</span><span class="o">:</span><span class="w">   </span><span class="n">tf</span><span class="o">::</span><span class="n">Pipe</span><span class="p">{</span><span class="n">tf</span><span class="o">::</span><span class="n">PipeType</span><span class="o">::</span><span class="n">SERIAL</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="o">&amp;</span><span class="n">buffer_1</span><span class="p">](</span><span class="n">tf</span><span class="o">::</span><span class="n">Pipeflow</span><span class="o">&amp;</span><span class="w"> </span><span class="n">pf</span><span class="p">)</span><span class="w"> </span><span class="k">mutable</span><span class="p">{</span>
<span class="mi">15</span><span class="o">:</span><span class="w">     </span><span class="c1">// generate only 4 scheduling tokens</span>
<span class="mi">16</span><span class="o">:</span><span class="w">     </span><span class="k">if</span><span class="p">(</span><span class="n">pf</span><span class="p">.</span><span class="n">token</span><span class="p">()</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">4</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="mi">17</span><span class="o">:</span><span class="w">       </span><span class="n">pf</span><span class="p">.</span><span class="n">stop</span><span class="p">();</span>
<span class="mi">18</span><span class="o">:</span><span class="w">     </span><span class="p">}</span>
<span class="mi">19</span><span class="o">:</span><span class="w">     </span><span class="c1">// save the result of this pipe into the buffer</span>
<span class="mi">20</span><span class="o">:</span><span class="w">     </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="mi">21</span><span class="o">:</span><span class="w">       </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;pipeline 1, pipe 0: input token = %zu</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">pf</span><span class="p">.</span><span class="n">token</span><span class="p">());</span>
<span class="mi">22</span><span class="o">:</span><span class="w">       </span><span class="n">buffer_1</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">pf</span><span class="p">.</span><span class="n">token</span><span class="p">();</span>
<span class="mi">23</span><span class="o">:</span><span class="w">     </span><span class="p">}</span>
<span class="mi">24</span><span class="o">:</span><span class="w">   </span><span class="p">}},</span>
<span class="mi">25</span><span class="o">:</span>
<span class="mi">26</span><span class="o">:</span><span class="w">   </span><span class="n">tf</span><span class="o">::</span><span class="n">Pipe</span><span class="p">{</span><span class="n">tf</span><span class="o">::</span><span class="n">PipeType</span><span class="o">::</span><span class="n">PARALLEL</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="o">&amp;</span><span class="n">buffer_1</span><span class="p">](</span><span class="n">tf</span><span class="o">::</span><span class="n">Pipeflow</span><span class="o">&amp;</span><span class="w"> </span><span class="n">pf</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="mi">27</span><span class="o">:</span><span class="w">     </span><span class="n">printf</span><span class="p">(</span>
<span class="mi">28</span><span class="o">:</span><span class="w">       </span><span class="s">&quot;pipeline 1, pipe 1: input buffer_1[%zu] = %d</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span>
<span class="mi">29</span><span class="o">:</span><span class="w">       </span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">(),</span><span class="w"> </span><span class="n">buffer_1</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span>
<span class="mi">30</span><span class="o">:</span><span class="w">     </span><span class="p">);</span>
<span class="mi">31</span><span class="o">:</span><span class="w">     </span><span class="c1">// propagate the previous result to this pipe by adding one</span>
<span class="mi">32</span><span class="o">:</span><span class="w">     </span><span class="n">buffer_1</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer_1</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<span class="mi">33</span><span class="o">:</span><span class="w">   </span><span class="p">}},</span>
<span class="mi">34</span><span class="o">:</span>
<span class="mi">35</span><span class="o">:</span><span class="w">   </span><span class="n">tf</span><span class="o">::</span><span class="n">Pipe</span><span class="p">{</span><span class="n">tf</span><span class="o">::</span><span class="n">PipeType</span><span class="o">::</span><span class="n">SERIAL</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="o">&amp;</span><span class="n">buffer_1</span><span class="p">](</span><span class="n">tf</span><span class="o">::</span><span class="n">Pipeflow</span><span class="o">&amp;</span><span class="w"> </span><span class="n">pf</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="mi">36</span><span class="o">:</span><span class="w">     </span><span class="n">printf</span><span class="p">(</span>
<span class="mi">37</span><span class="o">:</span><span class="w">       </span><span class="s">&quot;pipeline 1, pipe 2: input buffer_1[%zu] = %d</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span>
<span class="mi">38</span><span class="o">:</span><span class="w">       </span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">(),</span><span class="w"> </span><span class="n">buffer_1</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span>
<span class="mi">39</span><span class="o">:</span><span class="w">     </span><span class="p">);</span>
<span class="mi">40</span><span class="o">:</span><span class="w">     </span><span class="c1">// propagate the previous result to this pipe by adding one</span>
<span class="mi">41</span><span class="o">:</span><span class="w">     </span><span class="n">buffer_1</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer_1</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<span class="mi">42</span><span class="o">:</span><span class="w">   </span><span class="p">}}</span>
<span class="mi">43</span><span class="o">:</span><span class="w"> </span><span class="p">);</span>
<span class="mi">44</span><span class="o">:</span><span class="w">  </span>
<span class="mi">45</span><span class="o">:</span><span class="w"> </span><span class="c1">// the pipeline_2 consists of three pipes (serial-parallel-serial)</span>
<span class="mi">46</span><span class="o">:</span><span class="w"> </span><span class="c1">// and up to four concurrent scheduling tokens</span>
<span class="mi">47</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Pipeline</span><span class="w"> </span><span class="n">pl_2</span><span class="p">(</span><span class="n">num_lines</span><span class="p">,</span>
<span class="mi">48</span><span class="o">:</span><span class="w">   </span><span class="n">tf</span><span class="o">::</span><span class="n">Pipe</span><span class="p">{</span><span class="n">tf</span><span class="o">::</span><span class="n">PipeType</span><span class="o">::</span><span class="n">SERIAL</span><span class="p">,</span><span class="w"> </span>
<span class="mi">49</span><span class="o">:</span><span class="w">   </span><span class="p">[</span><span class="o">&amp;</span><span class="n">buffer_2</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">buffer_1</span><span class="p">](</span><span class="n">tf</span><span class="o">::</span><span class="n">Pipeflow</span><span class="o">&amp;</span><span class="w"> </span><span class="n">pf</span><span class="p">)</span><span class="w"> </span><span class="k">mutable</span><span class="p">{</span>
<span class="mi">50</span><span class="o">:</span><span class="w">     </span><span class="c1">// generate only 4 scheduling tokens</span>
<span class="mi">51</span><span class="o">:</span><span class="w">     </span><span class="k">if</span><span class="p">(</span><span class="n">pf</span><span class="p">.</span><span class="n">token</span><span class="p">()</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">4</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="mi">52</span><span class="o">:</span><span class="w">       </span><span class="n">pf</span><span class="p">.</span><span class="n">stop</span><span class="p">();</span>
<span class="mi">53</span><span class="o">:</span><span class="w">     </span><span class="p">}</span>
<span class="mi">54</span><span class="o">:</span><span class="w">     </span><span class="c1">// save the result of this pipe into the buffer</span>
<span class="mi">55</span><span class="o">:</span><span class="w">     </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="mi">56</span><span class="o">:</span><span class="w">       </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;pipeline 2, pipe 0: input value = %d</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">buffer_1</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]);</span>
<span class="mi">57</span><span class="o">:</span><span class="w">       </span><span class="n">buffer_2</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer_1</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()];</span>
<span class="mi">58</span><span class="o">:</span><span class="w">     </span><span class="p">}</span>
<span class="mi">59</span><span class="o">:</span><span class="w">   </span><span class="p">}},</span>
<span class="mi">60</span><span class="o">:</span>
<span class="mi">61</span><span class="o">:</span><span class="w">   </span><span class="n">tf</span><span class="o">::</span><span class="n">Pipe</span><span class="p">{</span><span class="n">tf</span><span class="o">::</span><span class="n">PipeType</span><span class="o">::</span><span class="n">PARALLEL</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="o">&amp;</span><span class="n">buffer_2</span><span class="p">](</span><span class="n">tf</span><span class="o">::</span><span class="n">Pipeflow</span><span class="o">&amp;</span><span class="w"> </span><span class="n">pf</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="mi">62</span><span class="o">:</span><span class="w">     </span><span class="n">printf</span><span class="p">(</span>
<span class="mi">63</span><span class="o">:</span><span class="w">       </span><span class="s">&quot;pipeline 2, pipe 1: input buffer_2[%zu] = %d</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span>
<span class="mi">64</span><span class="o">:</span><span class="w">       </span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">(),</span><span class="w"> </span><span class="n">buffer_2</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span>
<span class="mi">65</span><span class="o">:</span><span class="w">     </span><span class="p">);</span>
<span class="mi">66</span><span class="o">:</span><span class="w">     </span><span class="c1">// propagate the previous result to this pipe by adding 1</span>
<span class="mi">67</span><span class="o">:</span><span class="w">     </span><span class="n">buffer_2</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer_2</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<span class="mi">68</span><span class="o">:</span><span class="w">   </span><span class="p">}},</span>
<span class="mi">69</span><span class="o">:</span>
<span class="mi">70</span><span class="o">:</span><span class="w">   </span><span class="n">tf</span><span class="o">::</span><span class="n">Pipe</span><span class="p">{</span><span class="n">tf</span><span class="o">::</span><span class="n">PipeType</span><span class="o">::</span><span class="n">SERIAL</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="o">&amp;</span><span class="n">buffer_2</span><span class="p">](</span><span class="n">tf</span><span class="o">::</span><span class="n">Pipeflow</span><span class="o">&amp;</span><span class="w"> </span><span class="n">pf</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="mi">71</span><span class="o">:</span><span class="w">     </span><span class="n">printf</span><span class="p">(</span>
<span class="mi">72</span><span class="o">:</span><span class="w">       </span><span class="s">&quot;pipeline 2, pipe 2: input buffer_2[%zu] = %d</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span>
<span class="mi">73</span><span class="o">:</span><span class="w">       </span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">(),</span><span class="w"> </span><span class="n">buffer_2</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span>
<span class="mi">74</span><span class="o">:</span><span class="w">     </span><span class="p">);</span>
<span class="mi">75</span><span class="o">:</span><span class="w">     </span><span class="c1">// propagate the previous result to this pipe by adding 1</span>
<span class="mi">76</span><span class="o">:</span><span class="w">     </span><span class="n">buffer_2</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer_2</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<span class="mi">77</span><span class="o">:</span><span class="w">   </span><span class="p">}}</span>
<span class="mi">78</span><span class="o">:</span><span class="w"> </span><span class="p">);</span>
<span class="mi">79</span><span class="o">:</span>
<span class="mi">80</span><span class="o">:</span><span class="w"> </span><span class="c1">// build the pipeline graph using composition</span>
<span class="mi">81</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">pipeline_1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">composed_of</span><span class="p">(</span><span class="n">pl_1</span><span class="p">).</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;pipeline_1&quot;</span><span class="p">);</span>
<span class="mi">82</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">pipeline_2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">composed_of</span><span class="p">(</span><span class="n">pl_2</span><span class="p">).</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;pipeline_2&quot;</span><span class="p">);</span>
<span class="mi">83</span><span class="o">:</span>
<span class="mi">84</span><span class="o">:</span><span class="w"> </span><span class="c1">// specify the graph dependency</span>
<span class="mi">85</span><span class="o">:</span><span class="w"> </span><span class="n">pipeline_1</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">pipeline_2</span><span class="p">);</span>
<span class="mi">86</span><span class="o">:</span><span class="w">  </span>
<span class="mi">87</span><span class="o">:</span><span class="w"> </span><span class="c1">// execute the taskflow</span>
<span class="mi">88</span><span class="o">:</span><span class="w"> </span><span class="n">executor</span><span class="p">.</span><span class="n">run</span><span class="p">(</span><span class="n">taskflow</span><span class="p">).</span><span class="n">wait</span><span class="p">();</span></pre><p>Debrief:</p><ul><li>Line 8 defines the data storage (<code>num_lines</code> integers) for pipeline <code>pl_1</code></li><li>Line 9 defines the data storage (<code>num_lines</code> integers) for pipeline <code>pl_2</code></li><li>Lines 14-24 define the first serial pipe in <code>pl_1</code></li><li>Lines 26-33 define the second parallel pipe in <code>pl_1</code></li><li>Lines 35-42 define the third serial pipe in <code>pl_1</code></li><li>Lines 48-59 define the first serial pipe in <code>pl_2</code> that takes the results of <code>pl_1</code> as inputs</li><li>Lines 61-68 define the second parallel pipe in <code>pl_2</code></li><li>Lines 70-77 define the third serial pipe in <code>pl_2</code></li><li>Lines 81-82 define the pipeline graphs using composition</li><li>Line 85 defines the task dependency</li><li>Line 88 runs the taskflow</li></ul><p>The taskflow graph of this pipeline example is illustrated as follows:</p><div class="m-graph"><svg style="width: 71.000rem; height: 17.900rem;" viewBox="0.00 0.00 710.00 178.50">
<g transform="scale(1 1) rotate(0) translate(4 174.5)">
<title>Taskflow</title>
<g class="m-cluster">
<title>cluster_p0x7ffeaf4d98d8</title>
<polygon points="8,-8 8,-162.5 106,-162.5 106,-8 8,-8"/>
<text text-anchor="middle" x="57" y="-149" font-family="Helvetica,sans-Serif" font-size="10.00">Taskflow</text>
</g>
<g class="m-cluster">
<title>cluster_p0x7ffeaf4d9510</title>
<polygon points="114,-8 114,-162.5 400,-162.5 400,-8 114,-8"/>
<text text-anchor="middle" x="257" y="-149" font-family="Helvetica,sans-Serif" font-size="10.00">m2</text>
</g>
<g class="m-cluster">
<title>cluster_p0x7ffeaf4d95b0</title>
<polygon points="408,-8 408,-162.5 694,-162.5 694,-8 408,-8"/>
<text text-anchor="middle" x="551" y="-149" font-family="Helvetica,sans-Serif" font-size="10.00">m1</text>
</g>
<g class="m-node m-flat">
<title>p0x1da4f10</title>
<polygon points="98,-135.25 20,-135.25 16,-131.25 16,-99.25 94,-99.25 98,-103.25 98,-135.25"/>
<polyline points="94,-131.25 16,-131.25"/>
<polyline points="94,-131.25 94,-99.25"/>
<polyline points="94,-131.25 98,-135.25"/>
<text text-anchor="middle" x="57" y="-113.38" font-family="Helvetica,sans-Serif" font-size="10.00">pipeline_1 [m1]</text>
</g>
<g class="m-node m-flat">
<title>p0x1da4ff8</title>
<polygon points="98,-52 20,-52 16,-48 16,-16 94,-16 98,-20 98,-52"/>
<polyline points="94,-48 16,-48"/>
<polyline points="94,-48 94,-16"/>
<polyline points="94,-48 98,-52"/>
<text text-anchor="middle" x="57" y="-30.12" font-family="Helvetica,sans-Serif" font-size="10.00">pipeline_2 [m2]</text>
</g>
<g class="m-edge">
<title>p0x1da4f10&#45;&gt;p0x1da4ff8</title>
<path d="M57,-99.02C57,-88.75 57,-75.45 57,-63.64"/>
<polygon points="60.5,-64 57,-54 53.5,-64 60.5,-64"/>
</g>
<g class="m-node">
<title>p0x1da4a88</title>
<polygon points="257,-135.25 226.04,-117.25 257,-99.25 287.96,-117.25 257,-135.25"/>
<text text-anchor="middle" x="257" y="-113.38" font-family="Helvetica,sans-Serif" font-size="10.00">cond</text>
</g>
<g class="m-node m-flat">
<title>p0x1da4b70</title>
<polygon points="176,-52 122,-52 122,-48 118,-48 118,-44 122,-44 122,-24 118,-24 118,-20 122,-20 122,-16 176,-16 176,-52"/>
<polyline points="122,-48 126,-48 126,-44 122,-44"/>
<polyline points="122,-24 126,-24 126,-20 122,-20"/>
<text text-anchor="middle" x="149" y="-30.12" font-family="Helvetica,sans-Serif" font-size="10.00">rt&#45;0</text>
</g>
<g class="m-edge">
<title>p0x1da4a88&#45;&gt;p0x1da4b70</title>
<path stroke-dasharray="5,2" d="M244.19,-106.61C228.71,-94.97 202.12,-74.96 180.97,-59.05"/>
<polygon points="183.29,-56.42 173.2,-53.2 179.09,-62.01 183.29,-56.42"/>
<text text-anchor="middle" x="211.62" y="-71.75" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-node m-flat">
<title>p0x1da4c58</title>
<polygon points="248,-52 194,-52 194,-48 190,-48 190,-44 194,-44 194,-24 190,-24 190,-20 194,-20 194,-16 248,-16 248,-52"/>
<polyline points="194,-48 198,-48 198,-44 194,-44"/>
<polyline points="194,-24 198,-24 198,-20 194,-20"/>
<text text-anchor="middle" x="221" y="-30.12" font-family="Helvetica,sans-Serif" font-size="10.00">rt&#45;1</text>
</g>
<g class="m-edge">
<title>p0x1da4a88&#45;&gt;p0x1da4c58</title>
<path stroke-dasharray="5,2" d="M250.89,-102.47C246.05,-91.54 239.13,-75.91 233.16,-62.45"/>
<polygon points="236.51,-61.36 229.26,-53.63 230.11,-64.19 236.51,-61.36"/>
<text text-anchor="middle" x="243.62" y="-71.75" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
<g class="m-node m-flat">
<title>p0x1da4d40</title>
<polygon points="320,-52 266,-52 266,-48 262,-48 262,-44 266,-44 266,-24 262,-24 262,-20 266,-20 266,-16 320,-16 320,-52"/>
<polyline points="266,-48 270,-48 270,-44 266,-44"/>
<polyline points="266,-24 270,-24 270,-20 266,-20"/>
<text text-anchor="middle" x="293" y="-30.12" font-family="Helvetica,sans-Serif" font-size="10.00">rt&#45;2</text>
</g>
<g class="m-edge">
<title>p0x1da4a88&#45;&gt;p0x1da4d40</title>
<path stroke-dasharray="5,2" d="M263.11,-102.47C267.95,-91.54 274.87,-75.91 280.84,-62.45"/>
<polygon points="283.89,-64.19 284.74,-53.63 277.49,-61.36 283.89,-64.19"/>
<text text-anchor="middle" x="279.62" y="-71.75" font-family="Helvetica,sans-Serif" font-size="10.00">2</text>
</g>
<g class="m-node m-flat">
<title>p0x1da4e28</title>
<polygon points="392,-52 338,-52 338,-48 334,-48 334,-44 338,-44 338,-24 334,-24 334,-20 338,-20 338,-16 392,-16 392,-52"/>
<polyline points="338,-48 342,-48 342,-44 338,-44"/>
<polyline points="338,-24 342,-24 342,-20 338,-20"/>
<text text-anchor="middle" x="365" y="-30.12" font-family="Helvetica,sans-Serif" font-size="10.00">rt&#45;3</text>
</g>
<g class="m-edge">
<title>p0x1da4a88&#45;&gt;p0x1da4e28</title>
<path stroke-dasharray="5,2" d="M269.81,-106.61C285.29,-94.97 311.88,-74.96 333.03,-59.05"/>
<polygon points="334.91,-62.01 340.8,-53.2 330.71,-56.42 334.91,-62.01"/>
<text text-anchor="middle" x="319.62" y="-71.75" font-family="Helvetica,sans-Serif" font-size="10.00">3</text>
</g>
<g class="m-node">
<title>p0x1da4600</title>
<polygon points="551,-135.25 520.04,-117.25 551,-99.25 581.96,-117.25 551,-135.25"/>
<text text-anchor="middle" x="551" y="-113.38" font-family="Helvetica,sans-Serif" font-size="10.00">cond</text>
</g>
<g class="m-node m-flat">
<title>p0x1da46e8</title>
<polygon points="470,-52 416,-52 416,-48 412,-48 412,-44 416,-44 416,-24 412,-24 412,-20 416,-20 416,-16 470,-16 470,-52"/>
<polyline points="416,-48 420,-48 420,-44 416,-44"/>
<polyline points="416,-24 420,-24 420,-20 416,-20"/>
<text text-anchor="middle" x="443" y="-30.12" font-family="Helvetica,sans-Serif" font-size="10.00">rt&#45;0</text>
</g>
<g class="m-edge">
<title>p0x1da4600&#45;&gt;p0x1da46e8</title>
<path stroke-dasharray="5,2" d="M538.19,-106.61C522.71,-94.97 496.12,-74.96 474.97,-59.05"/>
<polygon points="477.29,-56.42 467.2,-53.2 473.09,-62.01 477.29,-56.42"/>
<text text-anchor="middle" x="505.62" y="-71.75" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-node m-flat">
<title>p0x1da47d0</title>
<polygon points="542,-52 488,-52 488,-48 484,-48 484,-44 488,-44 488,-24 484,-24 484,-20 488,-20 488,-16 542,-16 542,-52"/>
<polyline points="488,-48 492,-48 492,-44 488,-44"/>
<polyline points="488,-24 492,-24 492,-20 488,-20"/>
<text text-anchor="middle" x="515" y="-30.12" font-family="Helvetica,sans-Serif" font-size="10.00">rt&#45;1</text>
</g>
<g class="m-edge">
<title>p0x1da4600&#45;&gt;p0x1da47d0</title>
<path stroke-dasharray="5,2" d="M544.89,-102.47C540.05,-91.54 533.13,-75.91 527.16,-62.45"/>
<polygon points="530.51,-61.36 523.26,-53.63 524.11,-64.19 530.51,-61.36"/>
<text text-anchor="middle" x="537.62" y="-71.75" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
<g class="m-node m-flat">
<title>p0x1da48b8</title>
<polygon points="614,-52 560,-52 560,-48 556,-48 556,-44 560,-44 560,-24 556,-24 556,-20 560,-20 560,-16 614,-16 614,-52"/>
<polyline points="560,-48 564,-48 564,-44 560,-44"/>
<polyline points="560,-24 564,-24 564,-20 560,-20"/>
<text text-anchor="middle" x="587" y="-30.12" font-family="Helvetica,sans-Serif" font-size="10.00">rt&#45;2</text>
</g>
<g class="m-edge">
<title>p0x1da4600&#45;&gt;p0x1da48b8</title>
<path stroke-dasharray="5,2" d="M557.11,-102.47C561.95,-91.54 568.87,-75.91 574.84,-62.45"/>
<polygon points="577.89,-64.19 578.74,-53.63 571.49,-61.36 577.89,-64.19"/>
<text text-anchor="middle" x="573.62" y="-71.75" font-family="Helvetica,sans-Serif" font-size="10.00">2</text>
</g>
<g class="m-node m-flat">
<title>p0x1da49a0</title>
<polygon points="686,-52 632,-52 632,-48 628,-48 628,-44 632,-44 632,-24 628,-24 628,-20 632,-20 632,-16 686,-16 686,-52"/>
<polyline points="632,-48 636,-48 636,-44 632,-44"/>
<polyline points="632,-24 636,-24 636,-20 632,-20"/>
<text text-anchor="middle" x="659" y="-30.12" font-family="Helvetica,sans-Serif" font-size="10.00">rt&#45;3</text>
</g>
<g class="m-edge">
<title>p0x1da4600&#45;&gt;p0x1da49a0</title>
<path stroke-dasharray="5,2" d="M563.81,-106.61C579.29,-94.97 605.88,-74.96 627.03,-59.05"/>
<polygon points="628.91,-62.01 634.8,-53.2 624.71,-56.42 628.91,-62.01"/>
<text text-anchor="middle" x="613.62" y="-71.75" font-family="Helvetica,sans-Serif" font-size="10.00">3</text>
</g>
</g>
</svg>
</div><p>The following snippet shows one of the possible outputs:</p><pre class="m-code">pipeline<span class="w"> </span><span class="m">1</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">0</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span>
pipeline<span class="w"> </span><span class="m">1</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span>buffer_1<span class="o">[</span><span class="m">0</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span>
pipeline<span class="w"> </span><span class="m">1</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer_1<span class="o">[</span><span class="m">0</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span>
pipeline<span class="w"> </span><span class="m">1</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">0</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span>
pipeline<span class="w"> </span><span class="m">1</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span>buffer_1<span class="o">[</span><span class="m">1</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span>
pipeline<span class="w"> </span><span class="m">1</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer_1<span class="o">[</span><span class="m">1</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">2</span>
pipeline<span class="w"> </span><span class="m">1</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">0</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">2</span>
pipeline<span class="w"> </span><span class="m">1</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span>buffer_1<span class="o">[</span><span class="m">2</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">2</span>
pipeline<span class="w"> </span><span class="m">1</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer_1<span class="o">[</span><span class="m">2</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">3</span>
pipeline<span class="w"> </span><span class="m">1</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">0</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">3</span>
pipeline<span class="w"> </span><span class="m">1</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span>buffer_1<span class="o">[</span><span class="m">3</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">3</span>
pipeline<span class="w"> </span><span class="m">1</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer_1<span class="o">[</span><span class="m">3</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">4</span>
pipeline<span class="w"> </span><span class="m">2</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">2</span>
pipeline<span class="w"> </span><span class="m">2</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer_2<span class="o">[</span><span class="m">0</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">2</span>
pipeline<span class="w"> </span><span class="m">2</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">3</span>:<span class="w"> </span>input<span class="w"> </span>buffer_2<span class="o">[</span><span class="m">0</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">3</span>
pipeline<span class="w"> </span><span class="m">2</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">3</span>
pipeline<span class="w"> </span><span class="m">2</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer_2<span class="o">[</span><span class="m">1</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">3</span>
pipeline<span class="w"> </span><span class="m">2</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">3</span>:<span class="w"> </span>input<span class="w"> </span>buffer_2<span class="o">[</span><span class="m">1</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">4</span>
pipeline<span class="w"> </span><span class="m">2</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">4</span>
pipeline<span class="w"> </span><span class="m">2</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer_2<span class="o">[</span><span class="m">2</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">4</span>
pipeline<span class="w"> </span><span class="m">2</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">3</span>:<span class="w"> </span>input<span class="w"> </span>buffer_2<span class="o">[</span><span class="m">2</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">5</span>
pipeline<span class="w"> </span><span class="m">2</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">5</span>
pipeline<span class="w"> </span><span class="m">2</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer_2<span class="o">[</span><span class="m">3</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">5</span>
pipeline<span class="w"> </span><span class="m">2</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">3</span>:<span class="w"> </span>input<span class="w"> </span>buffer_2<span class="o">[</span><span class="m">3</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">6</span></pre><p>The output of pipelines <code>pl_1</code> and <code>pl_2</code> can be different from run to run because their second pipes are both parallel types. Due to the task dependency between <code>pipeline_1</code> and <code>pipeline_2</code>, the output of <code>pl_1</code> precedes the output of <code>pl_2</code>.</p></section><section id="DefineMultipleTaskParallelPipelines"><h3><a href="#DefineMultipleTaskParallelPipelines">Example 3: Define Multiple Parallel Pipelines</a></h3><p>This example creates two independent pipelines that run in parallel on different data sets.</p><pre class="m-code"><span class="w"> </span><span class="mi">1</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Taskflow</span><span class="w"> </span><span class="n">taskflow</span><span class="p">(</span><span class="s">&quot;pipeline&quot;</span><span class="p">);</span>
<span class="w"> </span><span class="mi">2</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Executor</span><span class="w"> </span><span class="n">executor</span><span class="p">;</span>
<span class="w"> </span><span class="mi">3</span><span class="o">:</span>
<span class="w"> </span><span class="mi">4</span><span class="o">:</span><span class="w"> </span><span class="c1">// define the maximum parallelism of the pipeline</span>
<span class="w"> </span><span class="mi">5</span><span class="o">:</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">size_t</span><span class="w"> </span><span class="n">num_lines</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">4</span><span class="p">;</span>
<span class="w"> </span><span class="mi">6</span><span class="o">:</span>
<span class="w"> </span><span class="mi">7</span><span class="o">:</span><span class="w"> </span><span class="c1">// custom data storage</span>
<span class="w"> </span><span class="mi">8</span><span class="o">:</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">array</span><span class="o">&lt;</span><span class="kt">int</span><span class="p">,</span><span class="w"> </span><span class="n">num_lines</span><span class="o">&gt;</span><span class="w"> </span><span class="n">buffer_1</span><span class="p">;</span>
<span class="w"> </span><span class="mi">9</span><span class="o">:</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">array</span><span class="o">&lt;</span><span class="kt">int</span><span class="p">,</span><span class="w"> </span><span class="n">num_lines</span><span class="o">&gt;</span><span class="w"> </span><span class="n">buffer_2</span><span class="p">;</span>
<span class="mi">10</span><span class="o">:</span><span class="w"> </span>
<span class="mi">11</span><span class="o">:</span><span class="w"> </span><span class="c1">// the pipeline_1 consists of three pipes (serial-parallel-serial)</span>
<span class="mi">12</span><span class="o">:</span><span class="w"> </span><span class="c1">// and up to four concurrent scheduling tokens</span>
<span class="mi">13</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Pipeline</span><span class="w"> </span><span class="n">pl_1</span><span class="p">(</span><span class="n">num_lines</span><span class="p">,</span>
<span class="mi">14</span><span class="o">:</span><span class="w">   </span><span class="n">tf</span><span class="o">::</span><span class="n">Pipe</span><span class="p">{</span><span class="n">tf</span><span class="o">::</span><span class="n">PipeType</span><span class="o">::</span><span class="n">SERIAL</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="o">&amp;</span><span class="n">buffer_1</span><span class="p">](</span><span class="n">tf</span><span class="o">::</span><span class="n">Pipeflow</span><span class="o">&amp;</span><span class="w"> </span><span class="n">pf</span><span class="p">)</span><span class="w"> </span><span class="k">mutable</span><span class="p">{</span>
<span class="mi">15</span><span class="o">:</span><span class="w">     </span><span class="c1">// generate only 5 scheduling tokens</span>
<span class="mi">16</span><span class="o">:</span><span class="w">     </span><span class="k">if</span><span class="p">(</span><span class="n">pf</span><span class="p">.</span><span class="n">token</span><span class="p">()</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">5</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="mi">17</span><span class="o">:</span><span class="w">       </span><span class="n">pf</span><span class="p">.</span><span class="n">stop</span><span class="p">();</span>
<span class="mi">18</span><span class="o">:</span><span class="w">     </span><span class="p">}</span>
<span class="mi">19</span><span class="o">:</span><span class="w">     </span><span class="c1">// save the result of this pipe into the buffer</span>
<span class="mi">20</span><span class="o">:</span><span class="w">     </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="mi">21</span><span class="o">:</span><span class="w">       </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;pipeline 1, pipe 0: input token = %zu</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">pf</span><span class="p">.</span><span class="n">token</span><span class="p">());</span>
<span class="mi">22</span><span class="o">:</span><span class="w">       </span><span class="n">buffer_1</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">pf</span><span class="p">.</span><span class="n">token</span><span class="p">();</span>
<span class="mi">23</span><span class="o">:</span><span class="w">     </span><span class="p">}</span>
<span class="mi">24</span><span class="o">:</span><span class="w">   </span><span class="p">}},</span>
<span class="mi">25</span><span class="o">:</span>
<span class="mi">26</span><span class="o">:</span><span class="w">   </span><span class="n">tf</span><span class="o">::</span><span class="n">Pipe</span><span class="p">{</span><span class="n">tf</span><span class="o">::</span><span class="n">PipeType</span><span class="o">::</span><span class="n">PARALLEL</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="o">&amp;</span><span class="n">buffer_1</span><span class="p">](</span><span class="n">tf</span><span class="o">::</span><span class="n">Pipeflow</span><span class="o">&amp;</span><span class="w"> </span><span class="n">pf</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="mi">27</span><span class="o">:</span><span class="w">     </span><span class="n">printf</span><span class="p">(</span>
<span class="mi">28</span><span class="o">:</span><span class="w">       </span><span class="s">&quot;pipeline 1, pipe 1: input buffer_1[%zu] = %d</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span>
<span class="mi">29</span><span class="o">:</span><span class="w">       </span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">(),</span><span class="w"> </span><span class="n">buffer_1</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span>
<span class="mi">30</span><span class="o">:</span><span class="w">     </span><span class="p">);</span>
<span class="mi">31</span><span class="o">:</span><span class="w">     </span><span class="c1">// propagate the previous result to this pipe by adding one</span>
<span class="mi">32</span><span class="o">:</span><span class="w">     </span><span class="n">buffer_1</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer_1</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<span class="mi">33</span><span class="o">:</span><span class="w">   </span><span class="p">}},</span>
<span class="mi">34</span><span class="o">:</span>
<span class="mi">35</span><span class="o">:</span><span class="w">   </span><span class="n">tf</span><span class="o">::</span><span class="n">Pipe</span><span class="p">{</span><span class="n">tf</span><span class="o">::</span><span class="n">PipeType</span><span class="o">::</span><span class="n">SERIAL</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="o">&amp;</span><span class="n">buffer_1</span><span class="p">](</span><span class="n">tf</span><span class="o">::</span><span class="n">Pipeflow</span><span class="o">&amp;</span><span class="w"> </span><span class="n">pf</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="mi">36</span><span class="o">:</span><span class="w">     </span><span class="n">printf</span><span class="p">(</span>
<span class="mi">37</span><span class="o">:</span><span class="w">       </span><span class="s">&quot;pipeline 1, pipe 2: input buffer_1[%zu] = %d</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span>
<span class="mi">38</span><span class="o">:</span><span class="w">       </span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">(),</span><span class="w"> </span><span class="n">buffer_1</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span>
<span class="mi">39</span><span class="o">:</span><span class="w">     </span><span class="p">);</span>
<span class="mi">40</span><span class="o">:</span><span class="w">     </span><span class="c1">// propagate the previous result to this pipe by adding one</span>
<span class="mi">41</span><span class="o">:</span><span class="w">     </span><span class="n">buffer_1</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer_1</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<span class="mi">42</span><span class="o">:</span><span class="w">   </span><span class="p">}}</span>
<span class="mi">43</span><span class="o">:</span><span class="w"> </span><span class="p">);</span>
<span class="mi">44</span><span class="o">:</span><span class="w">  </span>
<span class="mi">45</span><span class="o">:</span><span class="w"> </span><span class="c1">// the pipeline_2 consists of three pipes (serial-parallel-serial)</span>
<span class="mi">46</span><span class="o">:</span><span class="w"> </span><span class="c1">// and up to four concurrent scheduling tokens</span>
<span class="mi">47</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Pipeline</span><span class="w"> </span><span class="n">pl_2</span><span class="p">(</span><span class="n">num_lines</span><span class="p">,</span>
<span class="mi">48</span><span class="o">:</span><span class="w">   </span><span class="n">tf</span><span class="o">::</span><span class="n">Pipe</span><span class="p">{</span><span class="n">tf</span><span class="o">::</span><span class="n">PipeType</span><span class="o">::</span><span class="n">SERIAL</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="o">&amp;</span><span class="n">buffer_2</span><span class="p">](</span><span class="n">tf</span><span class="o">::</span><span class="n">Pipeflow</span><span class="o">&amp;</span><span class="w"> </span><span class="n">pf</span><span class="p">)</span><span class="w"> </span><span class="k">mutable</span><span class="p">{</span>
<span class="mi">49</span><span class="o">:</span><span class="w">     </span><span class="c1">// generate only 2 scheduling tokens</span>
<span class="mi">50</span><span class="o">:</span><span class="w">     </span><span class="k">if</span><span class="p">(</span><span class="n">pf</span><span class="p">.</span><span class="n">token</span><span class="p">()</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">5</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="mi">51</span><span class="o">:</span><span class="w">       </span><span class="n">pf</span><span class="p">.</span><span class="n">stop</span><span class="p">();</span>
<span class="mi">52</span><span class="o">:</span><span class="w">     </span><span class="p">}</span>
<span class="mi">53</span><span class="o">:</span><span class="w">     </span><span class="c1">// save the result of this pipe into the buffer</span>
<span class="mi">54</span><span class="o">:</span><span class="w">     </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="mi">55</span><span class="o">:</span><span class="w">       </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;pipeline 2, pipe 0: input token = %zu</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">pf</span><span class="p">.</span><span class="n">token</span><span class="p">());</span>
<span class="mi">56</span><span class="o">:</span><span class="w">       </span><span class="n">buffer_2</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;pipeline&quot;</span><span class="p">;</span>
<span class="mi">57</span><span class="o">:</span><span class="w">     </span><span class="p">}</span>
<span class="mi">58</span><span class="o">:</span><span class="w">   </span><span class="p">}},</span>
<span class="mi">59</span><span class="o">:</span>
<span class="mi">60</span><span class="o">:</span><span class="w">   </span><span class="n">tf</span><span class="o">::</span><span class="n">Pipe</span><span class="p">{</span><span class="n">tf</span><span class="o">::</span><span class="n">PipeType</span><span class="o">::</span><span class="n">PARALLEL</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="o">&amp;</span><span class="n">buffer_2</span><span class="p">](</span><span class="n">tf</span><span class="o">::</span><span class="n">Pipeflow</span><span class="o">&amp;</span><span class="w"> </span><span class="n">pf</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="mi">61</span><span class="o">:</span><span class="w">     </span><span class="n">printf</span><span class="p">(</span>
<span class="mi">62</span><span class="o">:</span><span class="w">       </span><span class="s">&quot;pipeline 2, pipe 1: input buffer_2[%zu] = %d</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span>
<span class="mi">63</span><span class="o">:</span><span class="w">       </span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">(),</span><span class="w"> </span><span class="n">buffer_2</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span>
<span class="mi">64</span><span class="o">:</span><span class="w">     </span><span class="p">);</span>
<span class="mi">65</span><span class="o">:</span><span class="w">     </span><span class="c1">// propagate the previous result to this pipe by concatenating &quot;_&quot;</span>
<span class="mi">66</span><span class="o">:</span><span class="w">     </span><span class="n">buffer_2</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer_2</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()];</span>
<span class="mi">67</span><span class="o">:</span><span class="w">   </span><span class="p">}},</span>
<span class="mi">68</span><span class="o">:</span>
<span class="mi">69</span><span class="o">:</span><span class="w">   </span><span class="n">tf</span><span class="o">::</span><span class="n">Pipe</span><span class="p">{</span><span class="n">tf</span><span class="o">::</span><span class="n">PipeType</span><span class="o">::</span><span class="n">SERIAL</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="o">&amp;</span><span class="n">buffer_2</span><span class="p">](</span><span class="n">tf</span><span class="o">::</span><span class="n">Pipeflow</span><span class="o">&amp;</span><span class="w"> </span><span class="n">pf</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="mi">70</span><span class="o">:</span><span class="w">     </span><span class="n">printf</span><span class="p">(</span>
<span class="mi">71</span><span class="o">:</span><span class="w">       </span><span class="s">&quot;pipeline 2, pipe 2: input buffer_2[%zu] = %d</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span>
<span class="mi">72</span><span class="o">:</span><span class="w">       </span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">(),</span><span class="w"> </span><span class="n">buffer_2</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span>
<span class="mi">73</span><span class="o">:</span><span class="w">     </span><span class="p">);</span>
<span class="mi">74</span><span class="o">:</span><span class="w">     </span><span class="c1">// propagate the previous result to this pipe by concatenating &quot;2&quot;</span>
<span class="mi">75</span><span class="o">:</span><span class="w">     </span><span class="n">buffer_2</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer_2</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()];</span>
<span class="mi">76</span><span class="o">:</span><span class="w">   </span><span class="p">}}</span>
<span class="mi">77</span><span class="o">:</span><span class="w"> </span><span class="p">);</span>
<span class="mi">78</span><span class="o">:</span>
<span class="mi">79</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">pipeline_1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">composed_of</span><span class="p">(</span><span class="n">pl_1</span><span class="p">)</span>
<span class="mi">80</span><span class="o">:</span><span class="w">                               </span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;pipeline_1&quot;</span><span class="p">);</span>
<span class="mi">81</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">pipeline_2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">composed_of</span><span class="p">(</span><span class="n">pl_2</span><span class="p">)</span>
<span class="mi">82</span><span class="o">:</span><span class="w">                               </span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;pipeline_2&quot;</span><span class="p">);</span>
<span class="mi">83</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">initial</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;initial&quot;</span><span class="p">;</span><span class="w"> </span><span class="p">})</span>
<span class="mi">84</span><span class="o">:</span><span class="w">                               </span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;initial&quot;</span><span class="p">);</span>
<span class="mi">85</span><span class="o">:</span>
<span class="mi">86</span><span class="o">:</span><span class="w"> </span><span class="n">initial</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">pipeline_1</span><span class="p">,</span><span class="w"> </span><span class="n">pipeline_2</span><span class="p">);</span>
<span class="mi">87</span><span class="o">:</span><span class="w">  </span>
<span class="mi">88</span><span class="o">:</span><span class="w"> </span><span class="n">executor</span><span class="p">.</span><span class="n">run</span><span class="p">(</span><span class="n">taskflow</span><span class="p">).</span><span class="n">wait</span><span class="p">();</span></pre><p>Debrief:</p><ul><li>Line 8 defines the data storage (<code>num_lines</code> integers) for pipeline <code>pl_1</code></li><li>Line 9 defines the data storage (<code>num_lines</code> integers) for pipeline <code>pl_2</code></li><li>Lines 14-24 define the first serial pipe in <code>pl_1</code></li><li>Lines 26-33 define the second parallel pipe in <code>pl_1</code></li><li>Lines 35-42 define the third serial pipe in <code>pl_1</code></li><li>Lines 48-58 define the first serial pipe in <code>pl_2</code></li><li>Lines 60-67 define the second parallel pipe in <code>pl_2</code></li><li>Lines 69-76 define the third serial pipe in <code>pl_2</code></li><li>Lines 79-82 define the pipeline graphs using composition</li><li>Lines 83-84 define a static task.</li><li>Line 86 defines the task dependency</li><li>Line 88 runs the taskflow</li></ul><p>The taskflow graph of this pipeline example is illustrated as follows:</p><div class="m-graph"><svg style="width: 81.000rem; height: 17.900rem;" viewBox="0.00 0.00 810.00 178.50">
<g transform="scale(1 1) rotate(0) translate(4 174.5)">
<title>Taskflow</title>
<g class="m-cluster">
<title>cluster_p0x7ffe35bde158</title>
<polygon points="8,-8 8,-162.5 206,-162.5 206,-8 8,-8"/>
<text text-anchor="middle" x="107" y="-149" font-family="Helvetica,sans-Serif" font-size="10.00">Taskflow</text>
</g>
<g class="m-cluster">
<title>cluster_p0x7ffe35bddd80</title>
<polygon points="214,-8 214,-162.5 500,-162.5 500,-8 214,-8"/>
<text text-anchor="middle" x="357" y="-149" font-family="Helvetica,sans-Serif" font-size="10.00">m2</text>
</g>
<g class="m-cluster">
<title>cluster_p0x7ffe35bdde20</title>
<polygon points="508,-8 508,-162.5 794,-162.5 794,-8 508,-8"/>
<text text-anchor="middle" x="651" y="-149" font-family="Helvetica,sans-Serif" font-size="10.00">m1</text>
</g>
<g class="m-node m-flat">
<title>p0x131af10</title>
<polygon points="98,-52 20,-52 16,-48 16,-16 94,-16 98,-20 98,-52"/>
<polyline points="94,-48 16,-48"/>
<polyline points="94,-48 94,-16"/>
<polyline points="94,-48 98,-52"/>
<text text-anchor="middle" x="57" y="-30.12" font-family="Helvetica,sans-Serif" font-size="10.00">pipeline_1 [m1]</text>
</g>
<g class="m-node m-flat">
<title>p0x131aff8</title>
<polygon points="198,-52 120,-52 116,-48 116,-16 194,-16 198,-20 198,-52"/>
<polyline points="194,-48 116,-48"/>
<polyline points="194,-48 194,-16"/>
<polyline points="194,-48 198,-52"/>
<text text-anchor="middle" x="157" y="-30.12" font-family="Helvetica,sans-Serif" font-size="10.00">pipeline_2 [m2]</text>
</g>
<g class="m-node m-flat">
<title>p0x131b0e0</title>
<ellipse cx="107" cy="-117.25" rx="27" ry="18"/>
<text text-anchor="middle" x="107" y="-113.38" font-family="Helvetica,sans-Serif" font-size="10.00">initial</text>
</g>
<g class="m-edge">
<title>p0x131b0e0&#45;&gt;p0x131af10</title>
<path d="M97.12,-100.2C90.43,-89.32 81.42,-74.69 73.64,-62.04"/>
<polygon points="76.74,-60.4 68.52,-53.71 70.78,-64.07 76.74,-60.4"/>
</g>
<g class="m-edge">
<title>p0x131b0e0&#45;&gt;p0x131aff8</title>
<path d="M116.88,-100.2C123.57,-89.32 132.58,-74.69 140.36,-62.04"/>
<polygon points="143.22,-64.07 145.48,-53.71 137.26,-60.4 143.22,-64.07"/>
</g>
<g class="m-node">
<title>p0x131aa88</title>
<polygon points="357,-135.25 326.04,-117.25 357,-99.25 387.96,-117.25 357,-135.25"/>
<text text-anchor="middle" x="357" y="-113.38" font-family="Helvetica,sans-Serif" font-size="10.00">cond</text>
</g>
<g class="m-node m-flat">
<title>p0x131ab70</title>
<polygon points="276,-52 222,-52 222,-48 218,-48 218,-44 222,-44 222,-24 218,-24 218,-20 222,-20 222,-16 276,-16 276,-52"/>
<polyline points="222,-48 226,-48 226,-44 222,-44"/>
<polyline points="222,-24 226,-24 226,-20 222,-20"/>
<text text-anchor="middle" x="249" y="-30.12" font-family="Helvetica,sans-Serif" font-size="10.00">rt&#45;0</text>
</g>
<g class="m-edge">
<title>p0x131aa88&#45;&gt;p0x131ab70</title>
<path stroke-dasharray="5,2" d="M344.19,-106.61C328.71,-94.97 302.12,-74.96 280.97,-59.05"/>
<polygon points="283.29,-56.42 273.2,-53.2 279.09,-62.01 283.29,-56.42"/>
<text text-anchor="middle" x="311.62" y="-71.75" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-node m-flat">
<title>p0x131ac58</title>
<polygon points="348,-52 294,-52 294,-48 290,-48 290,-44 294,-44 294,-24 290,-24 290,-20 294,-20 294,-16 348,-16 348,-52"/>
<polyline points="294,-48 298,-48 298,-44 294,-44"/>
<polyline points="294,-24 298,-24 298,-20 294,-20"/>
<text text-anchor="middle" x="321" y="-30.12" font-family="Helvetica,sans-Serif" font-size="10.00">rt&#45;1</text>
</g>
<g class="m-edge">
<title>p0x131aa88&#45;&gt;p0x131ac58</title>
<path stroke-dasharray="5,2" d="M350.89,-102.47C346.05,-91.54 339.13,-75.91 333.16,-62.45"/>
<polygon points="336.51,-61.36 329.26,-53.63 330.11,-64.19 336.51,-61.36"/>
<text text-anchor="middle" x="343.62" y="-71.75" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
<g class="m-node m-flat">
<title>p0x131ad40</title>
<polygon points="420,-52 366,-52 366,-48 362,-48 362,-44 366,-44 366,-24 362,-24 362,-20 366,-20 366,-16 420,-16 420,-52"/>
<polyline points="366,-48 370,-48 370,-44 366,-44"/>
<polyline points="366,-24 370,-24 370,-20 366,-20"/>
<text text-anchor="middle" x="393" y="-30.12" font-family="Helvetica,sans-Serif" font-size="10.00">rt&#45;2</text>
</g>
<g class="m-edge">
<title>p0x131aa88&#45;&gt;p0x131ad40</title>
<path stroke-dasharray="5,2" d="M363.11,-102.47C367.95,-91.54 374.87,-75.91 380.84,-62.45"/>
<polygon points="383.89,-64.19 384.74,-53.63 377.49,-61.36 383.89,-64.19"/>
<text text-anchor="middle" x="379.62" y="-71.75" font-family="Helvetica,sans-Serif" font-size="10.00">2</text>
</g>
<g class="m-node m-flat">
<title>p0x131ae28</title>
<polygon points="492,-52 438,-52 438,-48 434,-48 434,-44 438,-44 438,-24 434,-24 434,-20 438,-20 438,-16 492,-16 492,-52"/>
<polyline points="438,-48 442,-48 442,-44 438,-44"/>
<polyline points="438,-24 442,-24 442,-20 438,-20"/>
<text text-anchor="middle" x="465" y="-30.12" font-family="Helvetica,sans-Serif" font-size="10.00">rt&#45;3</text>
</g>
<g class="m-edge">
<title>p0x131aa88&#45;&gt;p0x131ae28</title>
<path stroke-dasharray="5,2" d="M369.81,-106.61C385.29,-94.97 411.88,-74.96 433.03,-59.05"/>
<polygon points="434.91,-62.01 440.8,-53.2 430.71,-56.42 434.91,-62.01"/>
<text text-anchor="middle" x="419.62" y="-71.75" font-family="Helvetica,sans-Serif" font-size="10.00">3</text>
</g>
<g class="m-node">
<title>p0x131a600</title>
<polygon points="651,-135.25 620.04,-117.25 651,-99.25 681.96,-117.25 651,-135.25"/>
<text text-anchor="middle" x="651" y="-113.38" font-family="Helvetica,sans-Serif" font-size="10.00">cond</text>
</g>
<g class="m-node m-flat">
<title>p0x131a6e8</title>
<polygon points="570,-52 516,-52 516,-48 512,-48 512,-44 516,-44 516,-24 512,-24 512,-20 516,-20 516,-16 570,-16 570,-52"/>
<polyline points="516,-48 520,-48 520,-44 516,-44"/>
<polyline points="516,-24 520,-24 520,-20 516,-20"/>
<text text-anchor="middle" x="543" y="-30.12" font-family="Helvetica,sans-Serif" font-size="10.00">rt&#45;0</text>
</g>
<g class="m-edge">
<title>p0x131a600&#45;&gt;p0x131a6e8</title>
<path stroke-dasharray="5,2" d="M638.19,-106.61C622.71,-94.97 596.12,-74.96 574.97,-59.05"/>
<polygon points="577.29,-56.42 567.2,-53.2 573.09,-62.01 577.29,-56.42"/>
<text text-anchor="middle" x="605.62" y="-71.75" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-node m-flat">
<title>p0x131a7d0</title>
<polygon points="642,-52 588,-52 588,-48 584,-48 584,-44 588,-44 588,-24 584,-24 584,-20 588,-20 588,-16 642,-16 642,-52"/>
<polyline points="588,-48 592,-48 592,-44 588,-44"/>
<polyline points="588,-24 592,-24 592,-20 588,-20"/>
<text text-anchor="middle" x="615" y="-30.12" font-family="Helvetica,sans-Serif" font-size="10.00">rt&#45;1</text>
</g>
<g class="m-edge">
<title>p0x131a600&#45;&gt;p0x131a7d0</title>
<path stroke-dasharray="5,2" d="M644.89,-102.47C640.05,-91.54 633.13,-75.91 627.16,-62.45"/>
<polygon points="630.51,-61.36 623.26,-53.63 624.11,-64.19 630.51,-61.36"/>
<text text-anchor="middle" x="637.62" y="-71.75" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
<g class="m-node m-flat">
<title>p0x131a8b8</title>
<polygon points="714,-52 660,-52 660,-48 656,-48 656,-44 660,-44 660,-24 656,-24 656,-20 660,-20 660,-16 714,-16 714,-52"/>
<polyline points="660,-48 664,-48 664,-44 660,-44"/>
<polyline points="660,-24 664,-24 664,-20 660,-20"/>
<text text-anchor="middle" x="687" y="-30.12" font-family="Helvetica,sans-Serif" font-size="10.00">rt&#45;2</text>
</g>
<g class="m-edge">
<title>p0x131a600&#45;&gt;p0x131a8b8</title>
<path stroke-dasharray="5,2" d="M657.11,-102.47C661.95,-91.54 668.87,-75.91 674.84,-62.45"/>
<polygon points="677.89,-64.19 678.74,-53.63 671.49,-61.36 677.89,-64.19"/>
<text text-anchor="middle" x="673.62" y="-71.75" font-family="Helvetica,sans-Serif" font-size="10.00">2</text>
</g>
<g class="m-node m-flat">
<title>p0x131a9a0</title>
<polygon points="786,-52 732,-52 732,-48 728,-48 728,-44 732,-44 732,-24 728,-24 728,-20 732,-20 732,-16 786,-16 786,-52"/>
<polyline points="732,-48 736,-48 736,-44 732,-44"/>
<polyline points="732,-24 736,-24 736,-20 732,-20"/>
<text text-anchor="middle" x="759" y="-30.12" font-family="Helvetica,sans-Serif" font-size="10.00">rt&#45;3</text>
</g>
<g class="m-edge">
<title>p0x131a600&#45;&gt;p0x131a9a0</title>
<path stroke-dasharray="5,2" d="M663.81,-106.61C679.29,-94.97 705.88,-74.96 727.03,-59.05"/>
<polygon points="728.91,-62.01 734.8,-53.2 724.71,-56.42 728.91,-62.01"/>
<text text-anchor="middle" x="713.62" y="-71.75" font-family="Helvetica,sans-Serif" font-size="10.00">3</text>
</g>
</g>
</svg>
</div><p>The following snippet shows one of the possible outputs:</p><pre class="m-code">initial
pipeline<span class="w"> </span><span class="m">2</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">0</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span>
pipeline<span class="w"> </span><span class="m">2</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span>buffer_2<span class="o">[</span><span class="m">0</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span>
pipeline<span class="w"> </span><span class="m">2</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer_2<span class="o">[</span><span class="m">0</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span>
pipeline<span class="w"> </span><span class="m">1</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">0</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span>
pipeline<span class="w"> </span><span class="m">1</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span>buffer_1<span class="o">[</span><span class="m">0</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span>
pipeline<span class="w"> </span><span class="m">1</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer_1<span class="o">[</span><span class="m">0</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span>
pipeline<span class="w"> </span><span class="m">1</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">0</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span>
pipeline<span class="w"> </span><span class="m">1</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span>buffer_1<span class="o">[</span><span class="m">1</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span>
pipeline<span class="w"> </span><span class="m">1</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">0</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">2</span>
pipeline<span class="w"> </span><span class="m">1</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span>buffer_1<span class="o">[</span><span class="m">2</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">2</span>
pipeline<span class="w"> </span><span class="m">1</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">0</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">3</span>
pipeline<span class="w"> </span><span class="m">1</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span>buffer_1<span class="o">[</span><span class="m">3</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">3</span>
pipeline<span class="w"> </span><span class="m">1</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">0</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">4</span>
pipeline<span class="w"> </span><span class="m">1</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span>buffer_1<span class="o">[</span><span class="m">0</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">4</span>
pipeline<span class="w"> </span><span class="m">2</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">0</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span>
pipeline<span class="w"> </span><span class="m">2</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span>buffer_2<span class="o">[</span><span class="m">1</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span>
pipeline<span class="w"> </span><span class="m">2</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">0</span>:<span class="w"> </span>input<span class="w"> </span>buffer_2<span class="o">[</span><span class="m">1</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">2</span>
pipeline<span class="w"> </span><span class="m">2</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">0</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">2</span>
pipeline<span class="w"> </span><span class="m">2</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span>buffer_2<span class="o">[</span><span class="m">2</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">2</span>
pipeline<span class="w"> </span><span class="m">2</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer_2<span class="o">[</span><span class="m">2</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">3</span>
pipeline<span class="w"> </span><span class="m">2</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">0</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">3</span>
pipeline<span class="w"> </span><span class="m">2</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span>buffer_2<span class="o">[</span><span class="m">3</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">3</span>
pipeline<span class="w"> </span><span class="m">2</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer_2<span class="o">[</span><span class="m">3</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">4</span>
pipeline<span class="w"> </span><span class="m">2</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">0</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">4</span>
pipeline<span class="w"> </span><span class="m">2</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span>buffer_2<span class="o">[</span><span class="m">0</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">4</span>
pipeline<span class="w"> </span><span class="m">2</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer_2<span class="o">[</span><span class="m">0</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">5</span>
pipeline<span class="w"> </span><span class="m">1</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer_1<span class="o">[</span><span class="m">1</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">2</span>
pipeline<span class="w"> </span><span class="m">1</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer_1<span class="o">[</span><span class="m">2</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">3</span>
pipeline<span class="w"> </span><span class="m">1</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer_1<span class="o">[</span><span class="m">3</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">4</span>
pipeline<span class="w"> </span><span class="m">1</span>,<span class="w"> </span>pipe<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer_1<span class="o">[</span><span class="m">0</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">5</span></pre><p>Because pipeline <code>pl_1</code> and pipeline <code>pl_2</code> are running in parallel, their outputs may interleave.</p></section></section><section id="ResetPipeline"><h2><a href="#ResetPipeline">Reset a Pipeline</a></h2><p>Our pipeline scheduling framework keeps a <em>stateful</em> number of scheduled tokens at each submitted run. You can reset the pipeline to the initial state using <a href="classtf_1_1Pipeline.html#a311d874b98de6f0def8a7d869e8d15bd" class="m-doc">tf::<wbr />Pipeline::<wbr />reset()</a>, where the number of scheduled tokens will start from zero in the next run. Borrowed from <a href="TaskParallelPipeline.html#IterateAPipeline" class="m-doc">Example 1: Iterate a Pipeline</a>, the program below resets the pipeline at the second iteration (inside the condition task) so the scheduling token will start from zero in the next run.</p><pre class="m-code"><span class="n">tf</span><span class="o">::</span><span class="n">Taskflow</span><span class="w"> </span><span class="n">taskflow</span><span class="p">(</span><span class="s">&quot;pipeline&quot;</span><span class="p">);</span>
<span class="n">tf</span><span class="o">::</span><span class="n">Executor</span><span class="w"> </span><span class="n">executor</span><span class="p">;</span>

<span class="c1">// define the maximum parallelism of the pipeline</span>
<span class="k">const</span><span class="w"> </span><span class="kt">size_t</span><span class="w"> </span><span class="n">num_lines</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">4</span><span class="p">;</span>

<span class="c1">// custom data storage</span>
<span class="n">std</span><span class="o">::</span><span class="n">array</span><span class="o">&lt;</span><span class="kt">int</span><span class="p">,</span><span class="w"> </span><span class="n">num_lines</span><span class="o">&gt;</span><span class="w"> </span><span class="n">buffer</span><span class="p">;</span>

<span class="c1">// the pipeline consists of three pipes (serial-parallel-serial)</span>
<span class="c1">// and up to four concurrent scheduling tokens</span>
<span class="n">tf</span><span class="o">::</span><span class="n">Pipeline</span><span class="w"> </span><span class="nf">pl</span><span class="p">(</span><span class="n">num_lines</span><span class="p">,</span>
<span class="w">  </span><span class="n">tf</span><span class="o">::</span><span class="n">Pipe</span><span class="p">{</span><span class="n">tf</span><span class="o">::</span><span class="n">PipeType</span><span class="o">::</span><span class="n">SERIAL</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="o">&amp;</span><span class="n">buffer</span><span class="p">](</span><span class="n">tf</span><span class="o">::</span><span class="n">Pipeflow</span><span class="o">&amp;</span><span class="w"> </span><span class="n">pf</span><span class="p">)</span><span class="w"> </span><span class="k">mutable</span><span class="p">{</span>
<span class="w">    </span><span class="c1">// generate only 5 scheduling tokens</span>
<span class="w">    </span><span class="k">if</span><span class="p">(</span><span class="n">pf</span><span class="p">.</span><span class="n">token</span><span class="p">()</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">5</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">      </span><span class="n">pf</span><span class="p">.</span><span class="n">stop</span><span class="p">();</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">    </span><span class="c1">// save the result of this pipe into the buffer</span>
<span class="w">    </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="w">      </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;pipe 0: input token = %zu</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">pf</span><span class="p">.</span><span class="n">token</span><span class="p">());</span>
<span class="w">      </span><span class="n">buffer</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">pf</span><span class="p">.</span><span class="n">token</span><span class="p">();</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">  </span><span class="p">}},</span>

<span class="w">  </span><span class="n">tf</span><span class="o">::</span><span class="n">Pipe</span><span class="p">{</span><span class="n">tf</span><span class="o">::</span><span class="n">PipeType</span><span class="o">::</span><span class="n">PARALLEL</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="o">&amp;</span><span class="n">buffer</span><span class="p">](</span><span class="n">tf</span><span class="o">::</span><span class="n">Pipeflow</span><span class="o">&amp;</span><span class="w"> </span><span class="n">pf</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="n">printf</span><span class="p">(</span>
<span class="w">      </span><span class="s">&quot;pipe 1: input buffer_1[%zu] = %d</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">(),</span><span class="w"> </span><span class="n">buffer</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span>
<span class="w">    </span><span class="p">);</span>
<span class="w">    </span><span class="c1">// propagate the previous result to this pipe by adding one</span>
<span class="w">    </span><span class="n">buffer</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<span class="w">  </span><span class="p">}},</span>

<span class="w">  </span><span class="n">tf</span><span class="o">::</span><span class="n">Pipe</span><span class="p">{</span><span class="n">tf</span><span class="o">::</span><span class="n">PipeType</span><span class="o">::</span><span class="n">SERIAL</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="o">&amp;</span><span class="n">buffer</span><span class="p">](</span><span class="n">tf</span><span class="o">::</span><span class="n">Pipeflow</span><span class="o">&amp;</span><span class="w"> </span><span class="n">pf</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="n">printf</span><span class="p">(</span>
<span class="w">      </span><span class="s">&quot;pipe 2: input buffer[%zu][%zu] = %d</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">(),</span><span class="w"> </span><span class="n">buffer</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span>
<span class="w">    </span><span class="p">);</span>
<span class="w">    </span><span class="c1">// propagate the previous result to this pipe by adding one</span>
<span class="w">    </span><span class="n">buffer</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="p">[</span><span class="n">pf</span><span class="p">.</span><span class="n">line</span><span class="p">()]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<span class="w">  </span><span class="p">}}</span>
<span class="p">);</span>
<span class="w"> </span>
<span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">conditional</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&amp;</span><span class="p">](){</span>
<span class="w">  </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="o">++</span><span class="n">N</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="mi">2</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="n">pl</span><span class="p">.</span><span class="n">reset</span><span class="p">();</span>
<span class="w">    </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;Rerun the pipeline</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w">  </span><span class="p">}</span>
<span class="w">  </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<span class="w">  </span><span class="p">}</span>
<span class="p">}).</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;conditional&quot;</span><span class="p">);</span>

<span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">pipeline</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">composed_of</span><span class="p">(</span><span class="n">pl</span><span class="p">)</span>
<span class="w">                            </span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;pipeline&quot;</span><span class="p">);</span>
<span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">initial</span><span class="w">  </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;initial&quot;</span><span class="p">;</span><span class="w"> </span><span class="p">})</span>
<span class="w">                            </span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;initial&quot;</span><span class="p">);</span>
<span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">stop</span><span class="w">     </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;stop&quot;</span><span class="p">;</span><span class="w"> </span><span class="p">})</span>
<span class="w">                            </span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;stop&quot;</span><span class="p">);</span>

<span class="n">initial</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">pipeline</span><span class="p">);</span>
<span class="n">pipeline</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">conditional</span><span class="p">);</span>
<span class="n">conditional</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">pipeline</span><span class="p">,</span><span class="w"> </span><span class="n">stop</span><span class="p">);</span>
<span class="w"> </span>
<span class="n">executor</span><span class="p">.</span><span class="n">run</span><span class="p">(</span><span class="n">taskflow</span><span class="p">).</span><span class="n">wait</span><span class="p">();</span></pre><p>The following snippet shows one of the possible outputs:</p><pre class="m-code">initial
pipe<span class="w"> </span><span class="m">0</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span>
pipe<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span>buffer_1<span class="o">[</span><span class="m">0</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span>
pipe<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer_1<span class="o">[</span><span class="m">0</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span>
pipe<span class="w"> </span><span class="m">0</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span>
pipe<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span>buffer_1<span class="o">[</span><span class="m">1</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span>
pipe<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer_1<span class="o">[</span><span class="m">1</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">2</span>
pipe<span class="w"> </span><span class="m">0</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">2</span>
pipe<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span>buffer_1<span class="o">[</span><span class="m">2</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">2</span>
pipe<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer_1<span class="o">[</span><span class="m">2</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">3</span>
pipe<span class="w"> </span><span class="m">0</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">3</span>
pipe<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span>buffer_1<span class="o">[</span><span class="m">3</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">3</span>
pipe<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer_1<span class="o">[</span><span class="m">3</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">4</span>
pipe<span class="w"> </span><span class="m">0</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">4</span>
pipe<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span>buffer_1<span class="o">[</span><span class="m">0</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">4</span>
pipe<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer_1<span class="o">[</span><span class="m">0</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">5</span>
Rerun<span class="w"> </span>the<span class="w"> </span>pipeline
pipe<span class="w"> </span><span class="m">0</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span>
pipe<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span>buffer_1<span class="o">[</span><span class="m">0</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span>
pipe<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer_1<span class="o">[</span><span class="m">0</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span>
pipe<span class="w"> </span><span class="m">0</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span>
pipe<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span>buffer_1<span class="o">[</span><span class="m">1</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span>
pipe<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer_1<span class="o">[</span><span class="m">1</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">2</span>
pipe<span class="w"> </span><span class="m">0</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">2</span>
pipe<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span>buffer_1<span class="o">[</span><span class="m">2</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">2</span>
pipe<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer_1<span class="o">[</span><span class="m">2</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">3</span>
pipe<span class="w"> </span><span class="m">0</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">3</span>
pipe<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span>buffer_1<span class="o">[</span><span class="m">3</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">3</span>
pipe<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer_1<span class="o">[</span><span class="m">3</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">4</span>
pipe<span class="w"> </span><span class="m">0</span>:<span class="w"> </span>input<span class="w"> </span><span class="nv">token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">4</span>
pipe<span class="w"> </span><span class="m">1</span>:<span class="w"> </span>input<span class="w"> </span>buffer_1<span class="o">[</span><span class="m">0</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">4</span>
pipe<span class="w"> </span><span class="m">2</span>:<span class="w"> </span>input<span class="w"> </span>buffer_1<span class="o">[</span><span class="m">0</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">5</span>
stop</pre><p>The output can be different from run to run, since the second pipe is a parallel type. At the second iteration from the condition task, we reset the pipeline so the token identifier starts from <code>0</code> rather than <code>5</code>.</p></section><section id="TaskParallelPipelineLearnMore"><h2><a href="#TaskParallelPipelineLearnMore">Learn More about Taskflow Pipeline</a></h2><p>Visit the following pages to learn more about pipeline:</p><ul><li><a href="TaskParallelScalablePipeline.html" class="m-doc">Task-parallel Scalable Pipeline</a></li><li><a href="DataParallelPipeline.html" class="m-doc">Data-parallel Pipeline</a></li><li><a href="TextProcessingPipeline.html" class="m-doc">Text Processing Pipeline</a></li><li><a href="GraphProcessingPipeline.html" class="m-doc">Graph Processing Pipeline</a></li><li><a href="TaskflowProcessingPipeline.html" class="m-doc">Taskflow Processing Pipeline</a></li></ul></section>
      </div>
    </div>
  </div>
</article></main>
<div class="m-doc-search" id="search">
  <a href="#!" onclick="return hideSearch()"></a>
  <div class="m-container">
    <div class="m-row">
      <div class="m-col-m-8 m-push-m-2">
        <div class="m-doc-search-header m-text m-small">
          <div><span class="m-label m-default">Tab</span> / <span class="m-label m-default">T</span> to search, <span class="m-label m-default">Esc</span> to close</div>
          <div id="search-symbolcount">&hellip;</div>
        </div>
        <div class="m-doc-search-content">
          <form>
            <input type="search" name="q" id="search-input" placeholder="Loading &hellip;" disabled="disabled" autofocus="autofocus" autocomplete="off" spellcheck="false" />
          </form>
          <noscript class="m-text m-danger m-text-center">Unlike everything else in the docs, the search functionality <em>requires</em> JavaScript.</noscript>
          <div id="search-help" class="m-text m-dim m-text-center">
            <p class="m-noindent">Search for symbols, directories, files, pages or
            modules. You can omit any prefix from the symbol or file path; adding a
            <code>:</code> or <code>/</code> suffix lists all members of given symbol or
            directory.</p>
            <p class="m-noindent">Use <span class="m-label m-dim">&darr;</span>
            / <span class="m-label m-dim">&uarr;</span> to navigate through the list,
            <span class="m-label m-dim">Enter</span> to go.
            <span class="m-label m-dim">Tab</span> autocompletes common prefix, you can
            copy a link to the result using <span class="m-label m-dim">⌘</span>
            <span class="m-label m-dim">L</span> while <span class="m-label m-dim">⌘</span>
            <span class="m-label m-dim">M</span> produces a Markdown link.</p>
          </div>
          <div id="search-notfound" class="m-text m-warning m-text-center">Sorry, nothing was found.</div>
          <ul id="search-results"></ul>
        </div>
      </div>
    </div>
  </div>
</div>
<script src="search-v2.js"></script>
<script src="searchdata-v2.js" async="async"></script>
<footer><nav>
  <div class="m-container">
    <div class="m-row">
      <div class="m-col-l-10 m-push-l-1">
        <p>Taskflow handbook is part of the <a href="https://taskflow.github.io">Taskflow project</a>, copyright © <a href="https://tsung-wei-huang.github.io/">Dr. Tsung-Wei Huang</a>, 2018&ndash;2024.<br />Generated by <a href="https://doxygen.org/">Doxygen</a> 1.9.6 and <a href="https://mcss.mosra.cz/">m.css</a>.</p>
      </div>
    </div>
  </div>
</nav></footer>
</body>
</html>
